}
main (){
+ var abc = "hello world";
var a = read;
var b = read();
var c = plus;
-test() {
- var x = 5;
- var y = 6;
- x = x + y;
- return y;
+main(){
+ var a = "hello world";
}
-main() {return;}
\ No newline at end of file
= IdentToken String // Identifier
| NumberToken Int // Integer
| CharToken Char // Character literal
+ | StringToken [Char]// String literal
//Keyword tokens
| VarToken // var keyword
| ReturnToken // return keyword
lexWord ">" BiggerToken <|> lexWord "!" ExclamationToken <|>
lexWord "-" DashToken <|>
//Number and identifier tokens
- lexNumber <|> lexIdentifier <|>
+ lexString <|> lexNumber <|> lexIdentifier <|>
(item '\n' >>| pure LexNL) <|>
//Whitespace
(satisfy isSpace >>| (pure $ LexSpace 0 1)) <|>
>>= \char->pure case char of
'a' = LexToken 4 (CharToken $ toChar 7)
'b' = LexToken 4 (CharToken '\b')
- 'b' = LexToken 4 (CharToken '\b')
'f' = LexToken 4 (CharToken '\f')
'n' = LexToken 4 (CharToken '\n')
- 'r' = LexToken 4 (CharToken '\t')
+ 'r' = LexToken 4 (CharToken '\r')
+ 't' = LexToken 4 (CharToken '\t')
'v' = LexToken 4 (CharToken '\v')
'\'' =LexToken 4 (CharToken '\'')
c = (LexItemError $ "Unknown escape: " +++ toString c)
+
+ lexString :: Parser Char LexItem
+ lexString = item '"' *> (
+ many (satisfy ((<>) '"'))) <* item '"' >>= \cs->
+ pure $ LexToken (length cs) (StringToken cs)
from StdString import class toString
from lex import :: LexerOutput, :: Error, :: Token, :: TokenValue, :: Pos
-from AST import :: AST
+from AST import :: AST, :: Expr
:: ParserOutput :== Either Error AST
parser :: [Token] -> ParserOutput
+makeStrExpr :: Pos [Char] -> Expr
implementation module parse
+import GenPrint
import StdString
import StdTuple
import StdList
import StdMisc
from Data.List import intercalate, replicate, instance Functor []
from Text import class Text(concat), instance Text String
-import GenPrint
import yard
import lex
parseBasicExpr :: Parser Token Expr
parseBasicExpr = peekPos >>= \pos ->
+ (trans2 (StringToken []) (\(StringToken cs)->makeStrExpr pos cs)) <|>
(TupleExpr pos <$> (parseTuple parseExpr)) <|>
parseBBraces parseExpr <|>
trans1 EmptyListToken (EmptyListExpr pos) <|>
pure $ FunExpr pos ident args fs) <|>
(VarExpr pos <$> parseVarDef)
+makeStrExpr :: Pos [Char] -> Expr
+makeStrExpr p [] = EmptyListExpr p
+makeStrExpr p [x:xs] = Op2Expr p (CharExpr zero x) BiCons (makeStrExpr p xs)
+
parseFunCall :: Parser Token (String, [Expr], [FieldSelector])
parseFunCall = liftM3 (\x y z->(x, y, z))
parseIdent
(pure tok) (fail <?> PositionalError line col
("ParseError: Unexpected token: " +++ printToString token))
where
- eq (IdentToken _) (IdentToken _) = True
+ eq (IdentToken _) (IdentToken _) = True
eq (NumberToken _) (NumberToken _) = True
- eq (CharToken _) (CharToken _) = True
+ eq (CharToken _) (CharToken _) = True
+ eq (StringToken _) (StringToken _) = True
eq x y = gEq {|*|} x y
parseSepList :: TokenValue (Parser Token a) -> Parser Token [a]
(liftM pure p) <|> pure empty
parseIdent :: Parser Token String
-parseIdent = trans2 (IdentToken "") (\(IdentToken e).toString e)
+parseIdent = trans2 (IdentToken "") (\(IdentToken e)->toString e)
//liftM only goes to liftM5
liftM6 f m1 m2 m3 m4 m5 m6 = f <$> m1 <*> m2 <*> m3 <*> m4 <*> m5 <*> m6
,
FunDecl zero "1printbool" ["x"] Nothing [] [
IfStmt (VarExpr zero (VarDef "x" []))
- [FunStmt "1printstr" [strOp2 $ fromString "True"] []]
- [FunStmt "1printstr" [strOp2 $ fromString "False"] []]
+ [FunStmt "1printstr" [makeStrExpr zero $ fromString "True"] []]
+ [FunStmt "1printstr" [makeStrExpr zero $ fromString "False"] []]
]]
- strOp2 :: [Char] -> Expr
- strOp2 [] = EmptyListExpr zero
- strOp2 [x:xs] = Op2Expr zero (CharExpr zero x) BiCons (strOp2 xs)
-
Start :: *World -> *World
Start w
# (args, w) = parseArgs w