X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/45409c781a9e35df68c43b1e2f028d30bf90c0a0..5f9668526491332f62c05ad831dbf6d5fdc2b6d0:/Toc.g4 diff --git a/Toc.g4 b/Toc.g4 index 95c9a0d..631e2c3 100644 --- a/Toc.g4 +++ b/Toc.g4 @@ -9,15 +9,15 @@ decl: varDecl varDecl: 'var' var; var: varName (':' type) ('=' expr)?; +varInit: varName (':' type) ('=' expr); -type: typeName; +type: typeName (typeModifier)*; +typeModifier: '*' | '[' NUMBER? ']'; funcDecl: 'func' func; func: funcName '(' parameter ')' (':' type) body; -parameter: (firstParameter (additionalParameter)*)?; -firstParameter: var; -additionalParameter: ',' var; +parameter: (var (',' var)*)?; body: '{' stmt* '}'; @@ -28,55 +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; + +whileStmt: 'while' expr body; + +assignStmt: identifierExpr '=' expr; returnStmt: 'return' expr; -expr: funcCall - | literal - | identifier - | subscript - | memberAccess +expr: funcExpr + | litExpr + | identifierExpr | parenExpr - | operatorExpr; - -nonOpExpr: funcCall - | literal - | identifier - | subscript - | memberAccess - | parenExpr; + | accessExpr + | opExpr; -nonSubscriptExpr: funcCall - | identifier - | memberAccess - | parenExpr; +/* 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 ')'; @@ -86,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]+;