Skip to content

Commit

Permalink
Merge pull request #5 from distributed-lab/dev
Browse files Browse the repository at this point in the history
Refactoring and typo fix
  • Loading branch information
Arvolear authored Oct 17, 2024
2 parents a1b003d + 0ece9bd commit 6440ff4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 59 deletions.
90 changes: 49 additions & 41 deletions grammar/LexerCircom.g4 → grammar/CircomLexer.g4
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
lexer grammar LexerCircom;
lexer grammar CircomLexer;

/*//////////////////////////////////////////////////////////////
COMMON STRUCTURES
//////////////////////////////////////////////////////////////*/

VERSION: NUMBER '.' NUMBER '.' NUMBER ;

SIGNAL_TYPE: INPUT | OUTPUT ;

SIGNAL: 'signal' ;
/*//////////////////////////////////////////////////////////////
EXPLICIT KEYWORDS DEFINITION
//////////////////////////////////////////////////////////////*/

INPUT: 'input' ;
PRAGMA: 'pragma' ;
CIRCOM: 'circom' ;

OUTPUT: 'output' ;
CUSTOM_TEMPLATES: 'custom_templates' ;

PUBLIC: 'public' ;
INCLUDE: 'include' ;

CUSTOM: 'custom' ;
PARALLEL: 'parallel' ;

TEMPLATE: 'template' ;
FUNCTION: 'function' ;

MAIN: 'main' ;
PUBLIC: 'public' ;
COMPONENT: 'component' ;

VAR: 'var' ;
SIGNAL: 'signal' ;

FUNCTION: 'function' ;

RETURN: 'return' ;
INPUT: 'input' ;
OUTPUT: 'output' ;

IF: 'if' ;

ELSE: 'else' ;

FOR: 'for' ;

WHILE: 'while' ;

DO: 'do' ;

LOG: 'log' ;

ASSERT: 'assert' ;

INCLUDE: 'include' ;

CUSTOM: 'custom' ;

PRAGMA: 'pragma' ;

CIRCOM: 'circom' ;

CUSTOM_TEMPLATES: 'custom_templates' ;

MAIN: 'main' ;
RETURN: 'return' ;

PARALLEL: 'parallel' ;
/*//////////////////////////////////////////////////////////////
SYMBOLS
//////////////////////////////////////////////////////////////*/

LP: '(' ;
RP: ')' ;
Expand All @@ -59,31 +60,31 @@ RB: ']' ;
LC: '{' ;
RC: '}' ;

COLON: ':' ;
SEMICOLON: ';' ;

DOT: '.' ;
COMMA: ',' ;

ASSIGNMENT: '=' ;

ASSIGNMENT_OP: '+=' | '-=' | '*=' | '**=' | '/=' | '\\=' | '%=' | '<<=' | '>>=' | '&=' | '^=' | '|=' ;

SELF_OP: '++' | '--' ;

LEFT_ASSIGNMENT: '<--' | '<==' ;
UNDERSCORE: '_' ;

RIGHT_ASSIGNMENT: '-->' | '==>' ;
/*//////////////////////////////////////////////////////////////
OPERATORS
//////////////////////////////////////////////////////////////*/

CONSTRAINT_EQ: '===' ;
TERNARY_CONDITION: '?' ;
TERNARY_ALTERNATIVE: ':' ;

QUESTION_MARK: '?' ;
EQ_CONSTRAINT: '===' ;
LEFT_CONSTRAINT: '<--' | '<==' ;
RIGHT_CONSTRAINT: '-->' | '==>' ;

UNDERSCORE: '_' ;
// Unary operators
SELF_OP: '++' | '--' ;

NOT: '!' ;
BNOT: '~' ;

// left to right associativity
POW: '**' ;

MUL: '*' ;
Expand All @@ -101,24 +102,31 @@ BAND: '&' ;
BXOR: '^' ;
BOR: '|' ;

// Require parentheses associativity
EQ: '==' ;
NEQ: '!=' ;
GT: '>' ;
LT: '<' ;
LE: '>=' ;
GE: '<=' ;
LE: '<=' ;
GE: '>=' ;

// left to right associativity
AND: '&&' ;
OR: '||' ;

// right to left associativity
ASSIGNMENT: '=' ;
ASSIGNMENT_WITH_OP: '+=' | '-=' | '*=' | '**=' | '/=' | '\\=' | '%=' | '<<=' | '>>=' | '&=' | '^=' | '|=' ;

ID : ID_SYMBOL* LETTER (LETTER|DIGIT|ID_SYMBOL)*;
fragment
LETTER : [a-zA-Z\u0080-\u00FF] ;
fragment
ID_SYMBOL : [_$] ;

NUMBER: DIGIT+ | HEX; // match integers
NUMBER: DIGIT+ | HEX;
fragment
DIGIT: [0-9] ; // match single digit // match single letter
DIGIT: [0-9] ;

HEX : '0' 'x' HEXDIGIT+ ;
fragment
Expand All @@ -128,7 +136,7 @@ STRING : '"' (ESC|.)*? '"' ;
fragment ESC: '\\' [btnrf"\\] ;
COMMENT
: '/*' .*? '*/' -> channel(HIDDEN) // match anything between /* and */
: '/*' .*? '*/' -> channel(HIDDEN)
;
LINE_COMMENT
Expand Down
34 changes: 17 additions & 17 deletions grammar/Circom.g4 → grammar/CircomParser.g4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
grammar Circom;
parser grammar CircomParser;

import LexerCircom;
options { tokenVocab=CircomLexer; }

circuit
: pragmaDeclaration* includeDeclaration* blockDeclaration* componentMainDeclaration?
Expand Down Expand Up @@ -33,7 +33,7 @@ functionStmt
: functionBlock #FuncBlock
| ID arrayDimension* SELF_OP ';' #FuncSelfOp
| varDeclaration ';' #FuncVarDeclaration
| identifier (ASSIGNMENT | ASSIGNMENT_OP) expression ';' #FuncAssignmentExpression
| identifier (ASSIGNMENT | ASSIGNMENT_WITH_OP) expression ';' #FuncAssignmentExpression
| '(' argsWithUnderscore ')' ASSIGNMENT ('(' expressionList ')' | expression) ';' #FuncVariadicAssignment
| 'if' parExpression functionStmt ('else' functionStmt)? #IfFuncStmt
| 'while' parExpression functionStmt #WhileFuncStmt
Expand Down Expand Up @@ -67,15 +67,15 @@ templateStmt
| componentDeclaration ';'
| blockInstantiation ';'
| identifier ASSIGNMENT expression ';'
| expression CONSTRAINT_EQ expression ';'
| element (LEFT_ASSIGNMENT | ASSIGNMENT_OP) expression ';'
| '(' element (',' element)* ')' LEFT_ASSIGNMENT '(' expression (',' expression)* ')' ';'
| expression RIGHT_ASSIGNMENT element ';'
| expression RIGHT_ASSIGNMENT '(' element (',' element)* ')' ';'
| '_' (ASSIGNMENT | LEFT_ASSIGNMENT) (expression | blockInstantiation) ';'
| (expression | blockInstantiation) RIGHT_ASSIGNMENT '_' ';'
| '(' argsWithUnderscore ')' (ASSIGNMENT | LEFT_ASSIGNMENT) ('(' expressionList ')' | blockInstantiation | expression) ';'
| blockInstantiation RIGHT_ASSIGNMENT '(' argsWithUnderscore ')' ';'
| expression EQ_CONSTRAINT expression ';'
| element (LEFT_CONSTRAINT | ASSIGNMENT_WITH_OP) expression ';'
| '(' element (',' element)* ')' LEFT_CONSTRAINT '(' expression (',' expression)* ')' ';'
| expression RIGHT_CONSTRAINT element ';'
| expression RIGHT_CONSTRAINT '(' element (',' element)* ')' ';'
| '_' (ASSIGNMENT | LEFT_CONSTRAINT) (expression | blockInstantiation) ';'
| (expression | blockInstantiation) RIGHT_CONSTRAINT '_' ';'
| '(' argsWithUnderscore ')' (ASSIGNMENT | LEFT_CONSTRAINT) ('(' expressionList ')' | blockInstantiation | expression) ';'
| blockInstantiation RIGHT_CONSTRAINT '(' argsWithUnderscore ')' ';'
| 'if' parExpression templateStmt ('else' templateStmt)?
| 'while' parExpression templateStmt
| 'for' '(' forControl ')' templateStmt
Expand All @@ -89,7 +89,7 @@ forControl: forInit ';' expression ';' forUpdate ;

forInit: 'var'? identifier (ASSIGNMENT rhsValue)? ;

forUpdate: ID (SELF_OP | ((ASSIGNMENT | ASSIGNMENT_OP) expression)) | SELF_OP ID ;
forUpdate: ID (SELF_OP | ((ASSIGNMENT | ASSIGNMENT_WITH_OP) expression)) | SELF_OP ID ;

parExpression: '(' expression ')' ;

Expand All @@ -102,7 +102,7 @@ expression
| expression op=('+' | '-') expression #BinaryExpression
| expression op=('<<' | '>>') expression #BinaryExpression
| expression op=('&' | '^' | '|') expression #BinaryExpression
| expression op=('==' | '!=' | '>' | '<' | '<=' | '>=' | '&&' | '||') expression #BinaryExpression
| expression op=('==' | '!=' | '>' | '<' | '>=' | '<=' | '&&' | '||') expression #BinaryExpression
| expression '?' expression ':' expression #TernaryExpression
;

Expand Down Expand Up @@ -130,7 +130,7 @@ signalDefinition: 'signal' SIGNAL_TYPE? tagList? identifier;
tagList: '{' args '}' ;

signalDeclaration
: signalDefinition (LEFT_ASSIGNMENT rhsValue)?
: signalDefinition (LEFT_CONSTRAINT rhsValue)?
| signalDefinition (',' identifier)*
;

Expand All @@ -152,8 +152,8 @@ rhsValue

componentCall
: '(' expressionList? ')'
| '(' ID LEFT_ASSIGNMENT expression (',' ID LEFT_ASSIGNMENT expression)* ')'
| '(' expression RIGHT_ASSIGNMENT ID (',' expression RIGHT_ASSIGNMENT ID)* ')'
| '(' ID LEFT_CONSTRAINT expression (',' ID LEFT_CONSTRAINT expression)* ')'
| '(' expression RIGHT_CONSTRAINT ID (',' expression RIGHT_CONSTRAINT ID)* ')'
;

blockInstantiation: 'parallel'? ID '(' expressionList? ')' componentCall? ;
Expand Down
2 changes: 1 addition & 1 deletion simple_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type SimpleListener struct {
*bindings.BaseCircomListener
*bindings.BaseCircomParserListener
parser antlr.Parser
}

Expand Down

0 comments on commit 6440ff4

Please sign in to comment.