1 /* François Thomasset -- INRIA Rocquencourt -- Octobre 2001 *)
3 (* Translation from Maple to MuPad : syntaxic specification of maple *)
6 Copyright © 2001-2002 François Thomasset, all rights reserved.
7 All of Dan Stanger's changes are Copyright © 2021 Dan Stanger, all rights reserved.
8 Copying is covered by the GNU General Public License (GPL).
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details. */
19 import java
.util
.List
;
20 import org
.antlr
.v4
.runtime
.ParserRuleContext
;
21 import org
.antlr
.v4
.runtime
.tree
.ErrorNode
;
22 import org
.antlr
.v4
.runtime
.tree
.TerminalNode
;
23 import org
.stringtemplate
.v4
.ST
;
24 import org
.stringtemplate
.v4
.STGroup
;
25 import org
.stringtemplate
.v4
.STGroupFile
;
27 public class mapleParserVisitorImpl
extends mapleParserBaseVisitor
<ST
>{
29 public ST
visitProgram(mapleParserParser
.ProgramContext ctx
){
31 ST t
= visit(ctx
.statseq());
36 public ST
visitStatseq(mapleParserParser
.StatseqContext ctx
){
38 ST t
= M2M
.theTemplates
.getInstanceOf("statseq");
39 for(mapleParserParser
.StatContext stat
: ctx
.stat()){
40 //System.out.println(stat.getText());
41 t
.add("stat",visit(stat
));
47 public ST
visitComment(mapleParserParser
.CommentContext ctx
){
48 ST t
= M2M
.theTemplates
.getInstanceOf("comment");
49 t
.add("e",ctx
.COMMENT().getText().substring(1));
53 public ST
visitProcStat(mapleParserParser
.ProcStatContext ctx
){
56 ST t
= M2M
.theTemplates
.getInstanceOf("proc");
57 t
.add("name",ctx
.nameseq().name().get(0).getText());
58 for(mapleParserParser
.OneparmContext parm
: ctx
.parmseq().oneparm()){
59 t
.add("parms", parm
.getText());
61 t
.add("locals", visit(ctx
.decls_proc()));
62 t
.add("statseq", s
=visit(ctx
.statseq()));
67 public ST
visitLocals_of_proc(mapleParserParser
.Locals_of_procContext ctx
) {
68 ST t
= M2M
.theTemplates
.getInstanceOf("local");
69 for(mapleParserParser
.NameContext name
: ctx
.nameseq().name()){
70 t
.add("names", name
.getText());
75 public ST
visitIfStat(mapleParserParser
.IfStatContext ctx
) {
76 ST t
= M2M
.theTemplates
.getInstanceOf("ifstat");
77 t
.add("expr",visit(ctx
.expr()));
78 t
.add("statseq",visit(ctx
.statseq()));
79 mapleParserParser
.Elif_clauseContext elifClause
= ctx
.elif_clause();
80 if(elifClause
!=null)t
.add("elifclause",visit(elifClause
));
81 mapleParserParser
.Else_clauseContext elseClause
= ctx
.else_clause();
82 if(elseClause
!=null)t
.add("elseclause",visit(elseClause
));
86 public ST
visitElse_clause(mapleParserParser
.Else_clauseContext ctx
) {
87 ST t
= M2M
.theTemplates
.getInstanceOf("elseclause");
88 t
.add("s",visit(ctx
.statseq()));
92 public ST
visitElif_clause(mapleParserParser
.Elif_clauseContext ctx
) {
93 ST e
= M2M
.theTemplates
.getInstanceOf("exprseq");
94 say("size" + ctx
.expr().size());
95 for(int i
=0; i
< ctx
.expr().size(); i
++)
97 ST t
= M2M
.theTemplates
.getInstanceOf("elifclause");
98 t
.add("x",visit1("ifelifstat","expr",ctx
.expr(i
)));
99 t
.add("i",visit(ctx
.statseq(i
)));
100 say("i "+i
+" "+t
.render());
106 public ST
visitIfExpr(mapleParserParser
.IfExprContext ctx
) {
107 ST t
= M2M
.theTemplates
.getInstanceOf("ifstat");
108 t
.add("expr",visit(ctx
.expr()));
109 t
.add("statseq",visit(ctx
.exprseq()));
110 mapleParserParser
.Elif_clauseContext elifClause
= ctx
.elif_clause();
111 if(elifClause
!=null)t
.add("elifclause",visit(elifClause
));
112 mapleParserParser
.Else_clauseContext elseClause
= ctx
.else_clause();
113 if(elseClause
!=null)t
.add("elseclause",visit(elseClause
));
118 public ST visitSeqWithPrefix(mapleParserParser.SeqWithPrefixContext ctx) {
119 ST t = M2M.theTemplates.getInstanceOf("notimplemented");
120 t.add("name","SeqWithPrefix");
125 public ST
visitSeqSansPrefix(mapleParserParser
.SeqSansPrefixContext ctx
) {
126 //say("SeqWithoutPrefix");
127 ST t
= M2M
.theTemplates
.getInstanceOf("seqsansprefix");
128 List
<mapleParserParser
.ExprContext
> l
= ctx
.exprseq().expr();
129 say("SeqWithoutPrefix"+l
.size());
130 if(l
.size()<1){say("l empty"); return t
; }
131 mapleParserParser
.ExprContext left
= l
.get(0);
132 if(left
== null){say("left null"); return t
; }
133 t
.add("left",visit1("seqsansprefix","left",left
));
134 if(l
.size()<2){say("r empty"); return t
; }
135 mapleParserParser
.ExprContext right
= l
.get(1);
136 if(right
== null){say("right null"); return t
; }
137 t
.add("right",visit1("seqsansprefix","right",right
));
142 public ST visitIfOne(mapleParserParser.IfOneContext ctx) {
143 ST t = M2M.theTemplates.getInstanceOf("ifone");
144 t.add("expr",visit(ctx.expr()));
145 t.add("statseq",visit(ctx.statseq()));
149 public ST
visitAssignStat(mapleParserParser
.AssignStatContext ctx
) {
150 //say("visitAssignStat");
151 ST t
= M2M
.theTemplates
.getInstanceOf("assign");
152 t
.add("lvalue",ctx
.nameseq().name().get(0).getText());
153 t
.add("exprseq",visit(ctx
.exprseq()));
158 public ST
visitExprseq(mapleParserParser
.ExprseqContext ctx
) {
160 ST t
= M2M
.theTemplates
.getInstanceOf("exprseq");
161 for(mapleParserParser
.ExprContext expr
: ctx
.expr()){
162 say("expr "+expr
.getText());
163 t
.add("e",visit1("expr","",expr
));
168 public ST
visitNameBracket(mapleParserParser
.NameBracketContext ctx
){
169 say("visitNameBracketContext");
170 ST t
= M2M
.theTemplates
.getInstanceOf("namebracket");
171 t
.add("n",visit(ctx
.name()));
172 for(mapleParserParser
.ExprContext expr
: ctx
.exprseq().expr()){
173 say("expr "+expr
.getText());
174 t
.add("e",visit1("expr","",expr
));
179 public ST
visitFunctionalOperatorExpr(mapleParserParser
.FunctionalOperatorExprContext ctx
) {
180 //say("visitFunctionalOperatorExpr");
181 ST t
= M2M
.theTemplates
.getInstanceOf("function");
182 t
.add("name",ctx
.name().getText());
184 mapleParserParser
.Functional_operatorContext f
= ctx
.functional_operator();
185 if(f
== null){say("f null"); return t
; }
186 List
<mapleParserParser
.ExprseqContext
> l
= f
.exprseq();
187 if(l
.isEmpty()){say("f empty"); return t
; }
188 mapleParserParser
.ExprseqContext e
= l
.get(0);
189 if(e
== null){say("e null"); return t
; }
190 List
<mapleParserParser
.ExprContext
> l1
= e
.expr();
191 for(mapleParserParser
.ExprContext expr
: l1
){
192 t
.add("args",visit1("visitFunctionalOperatorExpr", "<none>",expr
));
197 public ST
visitSetExpr(mapleParserParser
.SetExprContext ctx
) {
198 ST t
= M2M
.theTemplates
.getInstanceOf("setexpr");
199 if(ctx
.exprseq()==null || ctx
.exprseq().expr()==null){ return t
; };
200 for(mapleParserParser
.ExprContext expr
: ctx
.exprseq().expr()){
201 t
.add("args",visit(expr
));
206 public ST
visitListExpr(mapleParserParser
.ListExprContext ctx
) {
208 ST t
= M2M
.theTemplates
.getInstanceOf("listexpr");
209 if(ctx
.exprseq()==null || ctx
.exprseq().expr()==null){ return t
; };
210 for(mapleParserParser
.ExprContext expr
: ctx
.exprseq().expr()){
211 say("listexpr "+expr
.getText());
212 t
.add("args",visit1("listexpr","",expr
));
217 public ST
visitId(mapleParserParser
.IdContext ctx
) {
218 ST t
= M2M
.theTemplates
.getInstanceOf("name");
219 t
.add("text",ctx
.getText());
223 public ST
visitDoubleQuote(mapleParserParser
.DoubleQuoteContext ctx
) {
224 ST t
= M2M
.theTemplates
.getInstanceOf("name");
225 t
.add("text",ctx
.getText());
229 public ST
visitForBodyWhile(mapleParserParser
.ForBodyWhileContext ctx
) {
230 ST t
= M2M
.theTemplates
.getInstanceOf("forbodywhile");
231 t
.add("expr",visit(ctx
.expr()));
232 t
.add("statseq",visit(ctx
.statseq()));
236 public ST
visitFor_in_stmt(mapleParserParser
.For_in_stmtContext ctx
) {
237 ST t
= M2M
.theTemplates
.getInstanceOf("forin");
238 t
.add("name",visit(ctx
.name()));
239 t
.add("expr",visit(ctx
.expr()));
240 t
.add("body",visit(ctx
.for_body()));
245 public ST
visitForBodyDo(mapleParserParser
.ForBodyDoContext ctx
) {
246 ST t
= M2M
.theTemplates
.getInstanceOf("forbodydo");
247 t
.add("statseq",visit(ctx
.statseq()));
251 public ST
visitBackQuote(mapleParserParser
.BackQuoteContext ctx
) {
252 // Not sure how to tell the difference between back quote used as an
253 // indentifier and back quote used to delimit a string.
254 ST t
= M2M
.theTemplates
.getInstanceOf("name");
255 t
.add("text",ctx
.getText());
259 public ST
visitQuote(mapleParserParser
.QuoteContext ctx
) {
260 ST t
= M2M
.theTemplates
.getInstanceOf("name");
261 t
.add("text",ctx
.getText());
265 public ST
visitFloatExpr(mapleParserParser
.FloatExprContext ctx
) {
266 // Try to convert the string to a number and fix the format.
267 // Use the input on failure.
268 ST t
= M2M
.theTemplates
.getInstanceOf("number");
269 java
.math
.BigDecimal d
= null;
270 String s
= ctx
.getText();
272 d
= new java
.math
.BigDecimal(s
);
274 }catch(NumberFormatException e
){};
278 public ST
visit1(String visitor
, String op
, mapleParserParser
.ExprContext c
)
283 } catch(Exception e
){
284 say(visitor
+" "+op
+" "+e
.toString());
287 ST n
= M2M
.theTemplates
.getInstanceOf("isnull");
288 n
.add("name",visitor
);
294 public ST
binexp(String exptype
, String op
, mapleParserParser
.ExprContext l
, mapleParserParser
.ExprContext r
)
296 ST t
= M2M
.theTemplates
.getInstanceOf(exptype
);
298 t
.add("l",visit1(exptype
, op
, l
));
299 t
.add("r",visit1(exptype
, op
, r
));
303 public ST
visitExpoOp(mapleParserParser
.ExpoOpContext ctx
) {
304 return binexp("binexp", ctx
.OP
.getText(),ctx
.expr(0),ctx
.expr(1));
307 public ST
visitMultOp(mapleParserParser
.MultOpContext ctx
) {
308 return binexp("binexp", ctx
.OP
.getText(),ctx
.expr(0),ctx
.expr(1));
311 public ST
visitAddOp(mapleParserParser
.AddOpContext ctx
) {
312 return binexp("binexp", ctx
.OP
.getText(),ctx
.expr(0),ctx
.expr(1));
315 public ST
visitBinaryRelOp(mapleParserParser
.BinaryRelOpContext ctx
) {
316 // Not sure if this is the best place to change this.
317 String op
= ctx
.OP
.getText().equals("<>")?
"#":ctx
.OP
.getText();
318 return binexp("binexp", op
,ctx
.expr(0),ctx
.expr(1));
320 public ST
visitSetRelOp(mapleParserParser
.SetRelOpContext ctx
) {
321 return binexp("setrelexp", ctx
.OP
.getText(),ctx
.expr(0),ctx
.expr(1));
324 public ST
visitIntIntervalExpr(mapleParserParser
.IntIntervalExprContext ctx
) {
325 ST t
= M2M
.theTemplates
.getInstanceOf("intintervalexpr");
326 say("visitIntIntervalExpr");
327 t
.add("left",ctx
.INT(0).getText());
328 t
.add("right",ctx
.INT(1).getText());
332 public ST
visitProcExpr(mapleParserParser
.ProcExprContext ctx
){
333 ST t
= M2M
.theTemplates
.getInstanceOf("procexpr");
334 for(mapleParserParser
.OneparmContext parm
: ctx
.parmseq().oneparm()){
335 t
.add("parms", parm
.getText());
337 //t.add("expr",visit1("procexpr","<none>",ctx.expr()));
338 t
.add("locals", visit(ctx
.decls_proc()));
339 t
.add("expr", visit(ctx
.statseq()));
344 public ST
visitParenExpr(mapleParserParser
.ParenExprContext ctx
) {
345 ST t
= M2M
.theTemplates
.getInstanceOf("paren");
346 t
.add("e",visit(ctx
.exprseq().expr(0)));
350 public ST
visitIntExpr(mapleParserParser
.IntExprContext ctx
) {
351 // Try to convert the string to a number and fix the format.
352 // Use the input on failure.
353 ST t
= M2M
.theTemplates
.getInstanceOf("number");
354 java
.math
.BigInteger d
= null;
355 String s
= ctx
.getText();
357 d
= new java
.math
.BigInteger(s
);
359 }catch(NumberFormatException e
){};
363 public void say(String s
){ System
.out
.println(s
); System
.out
.flush(); }