Commit 50d7c590 authored by Jiawen Lyu's avatar Jiawen Lyu

add function key word

parent ca58f49a
This diff is collapsed.
......@@ -37,47 +37,49 @@ T__35=36
T__36=37
T__37=38
T__38=39
FLOAT=40
VAR=41
WS=42
COMMENT1=43
COMMENT2=44
T__39=40
FLOAT=41
VAR=42
WS=43
COMMENT1=44
COMMENT2=45
'pour'=1
'rends'=2
'fin'=3
':'=4
'repete'=5
'['=6
']'=7
'av'=8
're'=9
'td'=10
'tg'=11
'fcc'=12
'fcap'=13
'lc'=14
'bc'=15
'fpos'=16
','=17
'store'=18
'move'=19
'donne'=20
'"'=21
'si'=22
'tantque'=23
'*'=24
'/'=25
'+'=26
'-'=27
'>'=28
'<'=29
'<='=30
'>='=31
'!='=32
'=='=33
'hasard'=34
'cos'=35
'('=36
')'=37
'sin'=38
'loop'=39
'fin'=2
'function'=3
'rends'=4
':'=5
'repete'=6
'['=7
']'=8
'av'=9
're'=10
'td'=11
'tg'=12
'fcc'=13
'fcap'=14
'lc'=15
'bc'=16
'fpos'=17
','=18
'store'=19
'move'=20
'donne'=21
'"'=22
'si'=23
'tantque'=24
'('=25
')'=26
'*'=27
'/'=28
'+'=29
'-'=30
'>'=31
'<'=32
'<='=33
'>='=34
'!='=35
'=='=36
'hasard'=37
'cos'=38
'sin'=39
'loop'=40
This diff is collapsed.
......@@ -37,47 +37,49 @@ T__35=36
T__36=37
T__37=38
T__38=39
FLOAT=40
VAR=41
WS=42
COMMENT1=43
COMMENT2=44
T__39=40
FLOAT=41
VAR=42
WS=43
COMMENT1=44
COMMENT2=45
'pour'=1
'rends'=2
'fin'=3
':'=4
'repete'=5
'['=6
']'=7
'av'=8
're'=9
'td'=10
'tg'=11
'fcc'=12
'fcap'=13
'lc'=14
'bc'=15
'fpos'=16
','=17
'store'=18
'move'=19
'donne'=20
'"'=21
'si'=22
'tantque'=23
'*'=24
'/'=25
'+'=26
'-'=27
'>'=28
'<'=29
'<='=30
'>='=31
'!='=32
'=='=33
'hasard'=34
'cos'=35
'('=36
')'=37
'sin'=38
'loop'=39
'fin'=2
'function'=3
'rends'=4
':'=5
'repete'=6
'['=7
']'=8
'av'=9
're'=10
'td'=11
'tg'=12
'fcc'=13
'fcap'=14
'lc'=15
'bc'=16
'fpos'=17
','=18
'store'=19
'move'=20
'donne'=21
'"'=22
'si'=23
'tantque'=24
'('=25
')'=26
'*'=27
'/'=28
'+'=29
'-'=30
'>'=31
'<'=32
'<='=33
'>='=34
'!='=35
'=='=36
'hasard'=37
'cos'=38
'sin'=39
'loop'=40
......@@ -12,7 +12,7 @@ COMMENT2 : '/*' .*? '*/' -> skip;
declaration :
'pour' VAR (liste_parametres)? liste_instructions 'fin' # procedure
| 'pour' VAR (liste_parametres)? (liste_instructions)? 'rends' expr 'fin' # fonction
| 'pour' 'function' VAR (liste_parametres)? (liste_instructions)? 'rends' expr 'fin' # fonction
;
liste_parametres :
......
This diff is collapsed.
......@@ -37,47 +37,49 @@ T__35=36
T__36=37
T__37=38
T__38=39
FLOAT=40
VAR=41
WS=42
COMMENT1=43
COMMENT2=44
T__39=40
FLOAT=41
VAR=42
WS=43
COMMENT1=44
COMMENT2=45
'pour'=1
'fin'=2
'rends'=3
':'=4
'repete'=5
'['=6
']'=7
'av'=8
're'=9
'td'=10
'tg'=11
'fcc'=12
'fcap'=13
'lc'=14
'bc'=15
'fpos'=16
','=17
'store'=18
'move'=19
'donne'=20
'"'=21
'si'=22
'tantque'=23
'('=24
')'=25
'*'=26
'/'=27
'+'=28
'-'=29
'>'=30
'<'=31
'<='=32
'>='=33
'!='=34
'=='=35
'hasard'=36
'cos'=37
'sin'=38
'loop'=39
'function'=3
'rends'=4
':'=5
'repete'=6
'['=7
']'=8
'av'=9
're'=10
'td'=11
'tg'=12
'fcc'=13
'fcap'=14
'lc'=15
'bc'=16
'fpos'=17
','=18
'store'=19
'move'=20
'donne'=21
'"'=22
'si'=23
'tantque'=24
'('=25
')'=26
'*'=27
'/'=28
'+'=29
'-'=30
'>'=31
'<'=32
'<='=33
'>='=34
'!='=35
'=='=36
'hasard'=37
'cos'=38
'sin'=39
'loop'=40
This diff is collapsed.
This diff is collapsed.
......@@ -37,47 +37,49 @@ T__35=36
T__36=37
T__37=38
T__38=39
FLOAT=40
VAR=41
WS=42
COMMENT1=43
COMMENT2=44
T__39=40
FLOAT=41
VAR=42
WS=43
COMMENT1=44
COMMENT2=45
'pour'=1
'fin'=2
'rends'=3
':'=4
'repete'=5
'['=6
']'=7
'av'=8
're'=9
'td'=10
'tg'=11
'fcc'=12
'fcap'=13
'lc'=14
'bc'=15
'fpos'=16
','=17
'store'=18
'move'=19
'donne'=20
'"'=21
'si'=22
'tantque'=23
'('=24
')'=25
'*'=26
'/'=27
'+'=28
'-'=29
'>'=30
'<'=31
'<='=32
'>='=33
'!='=34
'=='=35
'hasard'=36
'cos'=37
'sin'=38
'loop'=39
'function'=3
'rends'=4
':'=5
'repete'=6
'['=7
']'=8
'av'=9
're'=10
'td'=11
'tg'=12
'fcc'=13
'fcap'=14
'lc'=15
'bc'=16
'fpos'=17
','=18
'store'=19
'move'=20
'donne'=21
'"'=22
'si'=23
'tantque'=24
'('=25
')'=26
'*'=27
'/'=28
'+'=29
'-'=30
'>'=31
'<'=32
'<='=33
'>='=34
'!='=35
'=='=36
'hasard'=37
'cos'=38
'sin'=39
'loop'=40
This diff is collapsed.
......@@ -216,70 +216,70 @@ public class LogoTreeVisitor extends LogoBaseVisitor<Integer> {
return 0;
}
// @Override
// public Integer visitProcedure(LogoParser.ProcedureContext ctx) {
// String nomProcedure = ctx.VAR().getText();
// //创建一个作用域,然后推入执行栈中,方便参数列表把记住所有的参数名
// TableSymboles currentTableSymboles = new TableSymboles();
// pileExecution.push(currentTableSymboles);
//
// Liste_instructionsContext listeInstructions = ctx.liste_instructions();
// //通过执行栈存储参数列表
// Integer createListeParamsSuccess;
// if (ctx.liste_parametres() != null) {
// //如果有参数传进来
// createListeParamsSuccess = visit(ctx.liste_parametres());
// } else {
// //如果没有参数
// createListeParamsSuccess = 0;
// }
//
// //获取到参数名之后,创建相应的procedure实例
// currentTableSymboles = pileExecution.pop();
// if (createListeParamsSuccess == 0) {
// Procedure newProcedure = new Procedure(nomProcedure, currentTableSymboles, listeInstructions);
// tableProcedures.put(nomProcedure, newProcedure);
//
// return 0;
// } else return -1;
// }
//
// @Override
// public Integer visitExecuteProcedure(LogoParser.ExecuteProcedureContext ctx) {
// //拿到过程的名字
// String nomProcedure = ctx.VAR().getText();
//
// //通过名字从tableProcedures拿到过程
// Procedure currentProcedure = tableProcedures.get(nomProcedure);
// Liste_instructionsContext listeInstructions = currentProcedure.getListeInstructions();
//
// //在执行过程的时候需要把作用域推入执行栈
// TableSymboles tableSymbolesLocale = currentProcedure.getTableSymbLocale();
//
// List<String> nomsParams = tableSymbolesLocale.getListeNomsParams();
// List<LogoParser.ExprContext> listeParamsValues = ctx.expr();
// int index = 0;
//
// //给符号表中的参数赋值
// for (LogoParser.ExprContext currentExpr : listeParamsValues) {
// if (index < nomsParams.size()) {
// Binome expr = evaluateExpr(currentExpr);
// Double exprValue = expr._2;
// String nomParam = nomsParams.get(index);
// tableSymbolesLocale.creerVar(nomParam, exprValue);
// index++;
// } else return -1;//在调用函数的时候参数的个数和申明函数时不一样
// }
// pileExecution.push(tableSymbolesLocale);
//
// //开始执行语句
// int doesSuccess = visit(listeInstructions);
// if (doesSuccess == 0) {
// pileExecution.pop();
// return 0;
// } else return -2;
//
// }
@Override
public Integer visitProcedure(LogoParser.ProcedureContext ctx) {
String nomProcedure = ctx.VAR().getText();
//创建一个作用域,然后推入执行栈中,方便参数列表把记住所有的参数名
TableSymboles currentTableSymboles = new TableSymboles();
pileExecution.push(currentTableSymboles);
Liste_instructionsContext listeInstructions = ctx.liste_instructions();
//通过执行栈存储参数列表
Integer createListeParamsSuccess;
if (ctx.liste_parametres() != null) {
//如果有参数传进来
createListeParamsSuccess = visit(ctx.liste_parametres());
} else {
//如果没有参数
createListeParamsSuccess = 0;
}
//获取到参数名之后,创建相应的procedure实例
currentTableSymboles = pileExecution.pop();
if (createListeParamsSuccess == 0) {
Procedure newProcedure = new Procedure(nomProcedure, currentTableSymboles, listeInstructions);
tableProcedures.put(nomProcedure, newProcedure);
return 0;
} else return -1;
}
@Override
public Integer visitExecuteProcedure(LogoParser.ExecuteProcedureContext ctx) {
//拿到过程的名字
String nomProcedure = ctx.VAR().getText();
//通过名字从tableProcedures拿到过程
Procedure currentProcedure = tableProcedures.get(nomProcedure);
Liste_instructionsContext listeInstructions = currentProcedure.getListeInstructions();
//在执行过程的时候需要把作用域推入执行栈
TableSymboles tableSymbolesLocale = currentProcedure.getTableSymbLocale();
List<String> nomsParams = tableSymbolesLocale.getListeNomsParams();
List<LogoParser.ExprContext> listeParamsValues = ctx.expr();
int index = 0;
//给符号表中的参数赋值
for (LogoParser.ExprContext currentExpr : listeParamsValues) {
if (index < nomsParams.size()) {
Binome expr = evaluateExpr(currentExpr);
Double exprValue = expr._2;
String nomParam = nomsParams.get(index);
tableSymbolesLocale.creerVar(nomParam, exprValue);
index++;
} else return -1;//在调用函数的时候参数的个数和申明函数时不一样
}
pileExecution.push(tableSymbolesLocale);
//开始执行语句
int doesSuccess = visit(listeInstructions);
if (doesSuccess == 0) {
pileExecution.pop();
return 0;
} else return -2;
}
@Override
public Integer visitFonction(LogoParser.FonctionContext ctx) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment