X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/b64d16088b29615d222d33450cf0315467400e59..5f9668526491332f62c05ad831dbf6d5fdc2b6d0:/Toc.g4 diff --git a/Toc.g4 b/Toc.g4 index 7b9ff97..631e2c3 100644 --- a/Toc.g4 +++ b/Toc.g4 @@ -9,8 +9,10 @@ decl: varDecl varDecl: 'var' var; var: varName (':' type) ('=' expr)?; +varInit: varName (':' type) ('=' expr); -type: typeName; +type: typeName (typeModifier)*; +typeModifier: '*' | '[' NUMBER? ']'; funcDecl: 'func' func; @@ -26,63 +28,60 @@ structVar: var; structMethod: func; -stmt: (varDecl - | conditional - | loop - | assignment - | returnStmt - | expr) ; +stmt: varDecl + | ifStmt + | switchStmt + | forStmt + | whileStmt + | assignStmt + | returnStmt + | expr; -conditional: ifCond; -ifCond: 'if' expr body; +ifStmt: 'if' expr body ('else' 'if' expr body)* ('else' body)?; -loop: whileLoop; -whileLoop: 'while' expr body; +switchStmt: 'switch' identifierExpr switchBody; +switchBody: '{' ('case' expr body)* '}'; -assignment: identifier '=' expr; +forStmt: 'for' (varInit | assignStmt) ',' expr ',' expr body; -returnStmt: 'return' expr; +whileStmt: 'while' expr body; -expr: funcCall - | literal - | identifier - | subscript - | memberAccess - | parenExpr - | operatorExpr; +assignStmt: identifierExpr '=' expr; -nonOpExpr: funcCall - | literal - | identifier - | subscript - | memberAccess - | parenExpr; +returnStmt: 'return' expr; -nonSubscriptExpr: funcCall - | literal - | identifier - | memberAccess - | parenExpr; +expr: funcExpr + | litExpr + | identifierExpr + | parenExpr + | accessExpr + | opExpr; -nonAccessExpr: funcCall - | literal - | identifier - | subscript - | parenExpr - | operatorExpr; +/* op */ +nonOpExpr: funcExpr + | litExpr + | identifierExpr + | parenExpr + | accessExpr; -funcCall: funcName '(' (expr (',' expr)*)? ')'; +/* lit access op */ +nonAccessExpr: funcExpr + | identifierExpr + | parenExpr; -operatorExpr: binaryOperator; -binaryOperator: nonOpExpr BINARY_OPERATOR nonOpExpr (BINARY_OPERATOR nonOpExpr)*; +funcExpr: funcName '(' (expr (',' expr)*)? ')'; -identifier: varName; +opExpr: binaryOp | prefixOp | postfixOp | ternaryOp; +binaryOp: nonOpExpr BINARY_OP nonOpExpr (BINARY_OP nonOpExpr)*; +prefixOp: PREFIX_OP nonOpExpr; +postfixOp: nonOpExpr POSTFIX_OP; +ternaryOp: nonOpExpr '?' expr ':' expr; -literal: INTLIT; +identifierExpr: varName; -subscript: nonSubscriptExpr '[' expr ']'; +litExpr: INT_LIT | DECIMAL_LIT | STRING_LIT | BOOL_LIT; -memberAccess: identifier '.' identifier; +accessExpr: nonAccessExpr ((('.' | '->') identifierExpr) | ('[' expr ']'))+; parenExpr: '(' expr ')'; @@ -92,11 +91,22 @@ typeName: NAME; structName: NAME; -BINARY_OPERATOR: - '+' | '-' | '*' | '/' - | '==' | '!=' - | '<' | '>'; -INTLIT: ('+' | '-')? [0-9]+; +POSTFIX_OP: + '++' | '--'; +PREFIX_OP: + [+!~&*-] | POSTFIX_OP; +BINARY_OP: + [+*/%&<|^>-] | + '==' | '!=' | '<=' | '>=' | '<' | '>' | + '<<' | '>>' | '||' | '&&' | '&=' | '|=' | '^=' | + '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%='; + +INT_LIT: ('+' | '-')? [0-9]+; +DECIMAL_LIT: ('+' | '-')* [0-9]+ '.' [0-9]+; +STRING_LIT: '"' [^"]* '"'; +BOOL_LIT: 'true' | 'false'; + NAME: ([a-z] | [A-Z] | [0-9])+; WS: [ \t\r\n]+ -> skip; NEWLINE: [\r\n]+; +NUMBER: [0-9]+;