X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Parsyntax.y;h=d7b5f7deeee2f694a080b55ae195af1ff5c40e40;hp=8bf7c6812d4ca3074be5f2e493c57b5359353263;hb=HEAD;hpb=33370917adde77f98b0955843b3222d9838a3fe8 diff --git a/Parsyntax.y b/Parsyntax.y index 8bf7c68..d7b5f7d 100644 --- a/Parsyntax.y +++ b/Parsyntax.y @@ -6,23 +6,24 @@ import Lexsyntax import ErrM } -%name pStms Stms -%name pExp Exp +%name pProgram Program -- no lexer declaration %monad { Err } { thenM } { returnM } %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 "<=") } '>' { PT _ (TS ">") } @@ -40,6 +41,7 @@ import ErrM 'print' { PT _ (TS "print") } 'readBool' { PT _ (TS "readBool") } 'readInt' { PT _ (TS "readInt") } + 'return' { PT _ (TS "return") } 'true' { PT _ (TS "true") } 'while' { PT _ (TS "while") } @@ -58,20 +60,30 @@ Bool : 'true' { True } | 'false' { False } +Type :: { Type } +Type : 'int' { TInt } + | 'bool' { TBool } + + +Program :: { Program } +Program : ListFuncStm { 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 } + | 'return' Exp ';' { SReturn $2 } Exp :: { Exp } -Exp : Exp1 BOp Exp1 { BExp $1 $2 $3 } - | Ident '=' Exp { EAss $1 $3 } +Exp : Ident '=' Exp { EAss $1 $3 } + | Exp1 Op0 Exp1 { compExp_ $1 $2 $3 } | Exp1 { $1 } @@ -90,15 +102,13 @@ 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 } - | Exp4 { $1 } - - -Exp4 :: { Exp } -Exp4 : '(' Exp ')' { $2 } + | Ident '(' ListExp ')' { EFunc $1 $3 } + | '(' Exp ')' { $2 } ListStm :: { [Stm] } @@ -106,12 +116,43 @@ ListStm : {- empty -} { [] } | ListStm Stm { flip (:) $1 $2 } -Stms :: { Stms } -Stms : ListStm { Program (reverse $1) } +ListExp :: { [Exp] } +ListExp : {- empty -} { [] } + | Exp { (:[]) $1 } + | Exp ',' ListExp { (:) $1 $3 } -BOp :: { BOp } -BOp : '<' { Lt } +Decl :: { Decl } +Decl : Type Ident { Decl $1 $2 } + + +ListDecl :: { [Decl] } +ListDecl : {- empty -} { [] } + | Decl { (:[]) $1 } + | Decl ',' ListDecl { (:) $1 $3 } + + +Func :: { Func } +Func : Type Ident '(' ListDecl ')' '{' ListStm '}' { Func $1 $2 $4 (reverse $7) } + + +ListFunc :: { [Func] } +ListFunc : {- empty -} { [] } + | ListFunc Func { flip (:) $1 $2 } + + +FuncStm :: { FuncStm } +FuncStm : Stm { S $1 } + | Func { F $1 } + + +ListFuncStm :: { [FuncStm] } +ListFuncStm : {- empty -} { [] } + | ListFuncStm FuncStm { flip (:) $1 $2 } + + +Op0 :: { Op } +Op0 : '<' { Lt } | '<=' { ELt } | '>' { Gt } | '>=' { EGt } @@ -132,11 +173,7 @@ Op2 : '*' { Times } Op :: { Op } Op : Op1 { $1 } | Op2 { $1 } - - -Type :: { Type } -Type : 'int' { TInt } - | 'bool' { TBool } + | Op0 { $1 } @@ -153,10 +190,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 }