X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Parsyntax.y;h=6613fa351325648a0f3cd8bff19ae301e6fbc25b;hp=6fef6bad9bb89672f5902fd0fdc9a14d732c349a;hb=d553df8dfdffca78342d6fae142ceded9cd64415;hpb=e9be0603d9dbd1caa6a0032cad0e39815cb8f38d diff --git a/Parsyntax.y b/Parsyntax.y index 6fef6ba..6613fa3 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,20 +59,29 @@ 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 } + | Type Ident '=' Exp ';' { SDecl $1 $2 $4 } + | Type Ident ';' { SDeclD $1 $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 } - | Stm ';' { $1 } Exp :: { Exp } -Exp : Exp1 BOp Exp1 { BExp $1 $2 $3 } +Exp : Ident '=' Exp { EAss $1 $3 } + | Exp1 Op0 Exp1 { compExp_ $1 $2 $3 } | Exp1 { $1 } @@ -89,10 +99,10 @@ Exp3 :: { Exp } Exp3 : Ident '++' { postIncr_ $1 } | Ident '--' { postDecr_ $1 } | Ident { EVar $1 } - | Ident '=' Exp { EAss $1 $3 } | Integer { EInt $1 } - | '-' Exp3 { ENeg $2 } | Bool { EBool $1 } + | '-' Exp3 { ENeg $2 } + | '!' Exp3 { ENot $2 } | 'readInt' { EReadI } | 'readBool' { EReadB } | '(' Exp ')' { $2 } @@ -103,12 +113,8 @@ ListStm : {- empty -} { [] } | ListStm Stm { flip (:) $1 $2 } -Stms :: { Stms } -Stms : ListStm { Program (reverse $1) } - - -BOp :: { BOp } -BOp : '<' { Lt } +Op0 :: { Op } +Op0 : '<' { Lt } | '<=' { ELt } | '>' { Gt } | '>=' { EGt } @@ -129,11 +135,7 @@ Op2 : '*' { Times } Op :: { Op } Op : Op1 { $1 } | Op2 { $1 } - - -Type :: { Type } -Type : 'int' { TInt } - | 'bool' { TBool } + | Op0 { $1 } @@ -150,10 +152,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 if_ e_ s_ = SIf e_ s_ SNoop -op1_ e1_ o_ e2_ = OpExp e1_ o_ e2_ -op2_ e1_ o_ e2_ = OpExp e1_ o_ e2_ +compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ +op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ +op2_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_ postIncr_ i_ = EPost i_ Plus postDecr_ i_ = EPost i_ Minus }