From: Mart Lubbers Date: Fri, 20 May 2016 18:05:31 +0000 (+0200) Subject: add string literals X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=e33962666f8af6a34432d57180eb322f4543eb78;p=cc1516.git add string literals --- diff --git a/examples/high.spl b/examples/high.spl index a6ce7de..65f8434 100644 --- a/examples/high.spl +++ b/examples/high.spl @@ -13,6 +13,7 @@ helloworld() { } main (){ + var abc = "hello world"; var a = read; var b = read(); var c = plus; diff --git a/examples/test.spl b/examples/test.spl index 199cb26..85acc01 100644 --- a/examples/test.spl +++ b/examples/test.spl @@ -1,7 +1,3 @@ -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 diff --git a/lex.dcl b/lex.dcl index 46c1713..535487d 100644 --- a/lex.dcl +++ b/lex.dcl @@ -10,6 +10,7 @@ from AST import :: Pos = IdentToken String // Identifier | NumberToken Int // Integer | CharToken Char // Character literal + | StringToken [Char]// String literal //Keyword tokens | VarToken // var keyword | ReturnToken // return keyword diff --git a/lex.icl b/lex.icl index aa34987..3e711c8 100644 --- a/lex.icl +++ b/lex.icl @@ -63,7 +63,7 @@ lexToken = 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)) <|> @@ -109,10 +109,15 @@ lexToken = >>= \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) diff --git a/parse.dcl b/parse.dcl index ad7edea..f2713f7 100644 --- a/parse.dcl +++ b/parse.dcl @@ -5,8 +5,9 @@ from Data.Maybe import :: Maybe 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 diff --git a/parse.icl b/parse.icl index bea680b..e13b870 100644 --- a/parse.icl +++ b/parse.icl @@ -1,5 +1,6 @@ implementation module parse +import GenPrint import StdString import StdTuple import StdList @@ -14,7 +15,6 @@ import Data.Func 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 @@ -127,6 +127,7 @@ parseExpr = //Operators in order of binding strength parseBasicExpr :: Parser Token Expr parseBasicExpr = peekPos >>= \pos -> + (trans2 (StringToken []) (\(StringToken cs)->makeStrExpr pos cs)) <|> (TupleExpr pos <$> (parseTuple parseExpr)) <|> parseBBraces parseExpr <|> trans1 EmptyListToken (EmptyListExpr pos) <|> @@ -139,6 +140,10 @@ parseExpr = //Operators in order of binding strength 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 @@ -193,9 +198,10 @@ satTok t = top >>= \tok=:({line,col},token) -> if (eq t token) (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] @@ -204,7 +210,7 @@ parseSepList sep p = (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 diff --git a/spl.icl b/spl.icl index 9976cc1..0f1c07a 100644 --- a/spl.icl +++ b/spl.icl @@ -46,14 +46,10 @@ preamble (AST fd) = AST (pre ++ fd) , 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