-
Notifications
You must be signed in to change notification settings - Fork 1
/
HLCostLan.g4
84 lines (62 loc) · 2.17 KB
/
HLCostLan.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
grammar HLCostLan;
prg : complexType* serviceDecl* functionDecl* init;
init: '('formalParams? ')''=''>' '{' stm '}';
serviceDecl: 'service'ID':''('(type(','type)*)?')''->'type';';
functionDecl : 'fn'ID'(' formalParams? ')' '->' (type)'{'stm'}' ;
stm :
| serviceCall
|'if' '(' expOrCall ')' '{'stm '}' 'else' '{' stm '}'
|'for' '('ID 'in' '(' NUMBER','exp ')' ')' '{' stm '}' /*check for list of ast.exp */
| letIn
| functionCall
| 'return' expPlus ;
serviceCall : 'call'ID'('(exp(','exp)*)?')' (';' stm)?;
functionCall : ID'('( exp (','exp)* )? ')';
letIn: 'let' (ID '=' expPlus)+ 'in' stm;
expOrCall : exp | serviceCall ;
expPlus : exp
| serviceCall
| functionCall
| ID '{' ID ':' expPlus (',' ID ':' expPlus)* '}'
;
exp: left= exp op= ('+'|'-') right= exp #binExp
| left= exp op= ('>'|'==' | '>=' |'!=') right= exp #binExp
| left= exp op= ('&&' |'*' | '/') right= exp #binExp
| ID'.'ID #callFun
| '"' STRING #stringExp
| NUMBER #valExp
| ID #derExp;
type : basictype | ID;
basictype: 'int'
| 'char'
| 'string'
| 'bool'
| 'float'
| 'any'
| 'void';
// `<type> <= any` for all types
complexType : 'struct'ID'{' ID ':' (structType) (',' ID ':' (structType))* '}'
| ID ':' type'['NUMBER']';
structType : 'array''['typeArr']' | type;
typeArr : type;
params : ID',' params | ID ;
formalParams: ID ':' type(','ID ':' type)*;
//IDs
fragment CHAR : 'a'..'z' |'A'..'Z' ;
ID : CHAR (CHAR | DIGIT)* ;
//Numbers
fragment DIGIT : '0'..'9';
NUMBER : DIGIT+;
//String
STRING : ID(ID | ' ')* CLS_STR ;
CLS_STR : '"' ;
//ESCAPE SEQUENCES
NEWLINE : ( '\r\n' // DOS
| '\r' // MAC
| '\n' // Unix
)
->skip;
LINECOMMENTS : '//' (~('\n'|'\r'))* -> skip;
WS : (' '|'\t')+ -> skip;
BLOCKCOMMENTS : '/*'( ~('/'|'*')|'/'~'*'|'*'~'/'|BLOCKCOMMENTS)* '*/' -> skip;
LINE_NUMBER : [0-9]+;