X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Parsyntax.y;h=02e94b0bc4803b1a1ac5ca5f525de11cc3e348f1;hp=dbd776705fe7fbb566e77bf45bda1e20787638ef;hb=565fbd61dca527c23888e08783d0d91cee458524;hpb=6103c0359b95557566aa9154efa5bcd4d9c996f6 diff --git a/Parsyntax.y b/Parsyntax.y index dbd7767..02e94b0 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -14,15 +14,16 @@ import ErrM %tokentype { Token } %token - '=' { PT _ (TS "=") } ';' { PT _ (TS ";") } '{' { PT _ (TS "{") } '}' { PT _ (TS "}") } + '=' { PT _ (TS "=") } '(' { PT _ (TS "(") } ')' { PT _ (TS ")") } '++' { PT _ (TS "++") } '--' { PT _ (TS "--") } '-' { PT _ (TS "-") } + '!' { PT _ (TS "!") } '<' { PT _ (TS "<") } '<=' { PT _ (TS "<=") } '>' { PT _ (TS ">") } @@ -58,14 +59,25 @@ Bool : 'true' { True } | 'false' { False } +Type :: { Type } +Type : 'int' { TInt } + | 'bool' { TBool } + + +Stms :: { Stms } +Stms : ListStm { Program (reverse $1) } + + Stm :: { Stm } -Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 } - | Type Ident ';' { decl_ $1 $2 } - | Exp ';' { SExp $1 } +Stm : Exp ';' { SExp $1 } | '{' ListStm '}' { SBlock (reverse $2) } - | 'if' '(' Exp ')' Stm { if_ $3 $5 } - | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 } + | 'int' Ident '=' Exp ';' { declIntE_ $2 $4 } + | 'bool' Ident '=' Exp ';' { declBoolE_ $2 $4 } + | 'int' Ident ';' { declInt_ $2 } + | 'bool' Ident ';' { declBool_ $2 } | 'while' '(' Exp ')' Stm { SWhile $3 $5 } + | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 } + | 'if' '(' Exp ')' Stm { if_ $3 $5 } | 'print' Exp ';' { SPrint $2 } @@ -90,8 +102,9 @@ Exp3 : Ident '++' { postIncr_ $1 } | Ident '--' { postDecr_ $1 } | Ident { EVar $1 } | Integer { EInt $1 } - | '-' Exp3 { ENeg $2 } | Bool { EBool $1 } + | '-' Exp3 { ENeg $2 } + | '!' Exp3 { ENot $2 } | 'readInt' { EReadI } | 'readBool' { EReadB } | '(' Exp ')' { $2 } @@ -102,10 +115,6 @@ ListStm : {- empty -} { [] } | ListStm Stm { flip (:) $1 $2 } -Stms :: { Stms } -Stms : ListStm { Program (reverse $1) } - - Op0 :: { Op } Op0 : '<' { Lt } | '<=' { ELt } @@ -131,11 +140,6 @@ Op : Op1 { $1 } | Op0 { $1 } -Type :: { Type } -Type : 'int' { TInt } - | 'bool' { TBool } - - { @@ -150,7 +154,10 @@ happyError ts = Bad $ "syntax error at " ++ tokenPos ts ++ if null ts then [] else (" before " ++ unwords (map prToken (take 4 ts))) myLexer = tokens -decl_ t_ v_ = SDecl t_ v_ EDefault +declIntE_ x_ e_ = SDecl TInt x_ e_ +declBoolE_ x_ e_ = SDecl TBool x_ e_ +declInt_ x_ = SDecl TInt x_ (EInt 0) +declBool_ x_ = SDecl TBool x_ (EBool False) if_ e_ s_ = SIf e_ s_ SNoop compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_