Use 1//2 instead of ((rat simp) 1 2)
[maxima.git] / share / translators / m2mj / mapleParserVisitorImpl.java
blob5bd7c7d9197eb19668640f039ffe0d506c8f7154
1 /* François Thomasset -- INRIA Rocquencourt -- Octobre 2001 *)
3 (* Translation from Maple to MuPad : syntaxic specification of maple *)
5 (*
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>{
28 @Override
29 public ST visitProgram(mapleParserParser.ProgramContext ctx){
30 //say("program");
31 ST t = visit(ctx.statseq());
32 //say(t.render());
33 return t;
35 @Override
36 public ST visitStatseq(mapleParserParser.StatseqContext ctx){
37 //say("statseq");
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));
43 //t.inspect();
44 return t;
46 @Override
47 public ST visitComment(mapleParserParser.CommentContext ctx){
48 ST t = M2M.theTemplates.getInstanceOf("comment");
49 t.add("e",ctx.COMMENT().getText().substring(1));
50 return t;
52 @Override
53 public ST visitProcStat(mapleParserParser.ProcStatContext ctx){
54 //say("visitproc");
55 ST s=null;
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()));
63 //s.inspect();
64 return t;
66 @Override
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());
72 return t;
74 @Override
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));
83 return t;
85 @Override
86 public ST visitElse_clause(mapleParserParser.Else_clauseContext ctx) {
87 ST t = M2M.theTemplates.getInstanceOf("elseclause");
88 t.add("s",visit(ctx.statseq()));
89 return t;
91 @Override
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());
101 e.add("e",t);
103 return e;
105 @Override
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));
114 return t;
117 @Override
118 public ST visitSeqWithPrefix(mapleParserParser.SeqWithPrefixContext ctx) {
119 ST t = M2M.theTemplates.getInstanceOf("notimplemented");
120 t.add("name","SeqWithPrefix");
121 return t;
124 @Override
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));
138 return t;
141 @Override
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()));
146 return t;
147 } */
148 @Override
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()));
154 //say(t.render());
155 return t;
157 @Override
158 public ST visitExprseq(mapleParserParser.ExprseqContext ctx) {
159 say("visitExprseq");
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));
165 return t;
167 @Override
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));
176 return t;
178 @Override
179 public ST visitFunctionalOperatorExpr(mapleParserParser.FunctionalOperatorExprContext ctx) {
180 //say("visitFunctionalOperatorExpr");
181 ST t = M2M.theTemplates.getInstanceOf("function");
182 t.add("name",ctx.name().getText());
183 say(t.render());
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));
194 return t;
196 @Override
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));
203 return t;
205 @Override
206 public ST visitListExpr(mapleParserParser.ListExprContext ctx) {
207 say("listexpr");
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));
214 return t;
216 @Override
217 public ST visitId(mapleParserParser.IdContext ctx) {
218 ST t = M2M.theTemplates.getInstanceOf("name");
219 t.add("text",ctx.getText());
220 return t;
222 @Override
223 public ST visitDoubleQuote(mapleParserParser.DoubleQuoteContext ctx) {
224 ST t = M2M.theTemplates.getInstanceOf("name");
225 t.add("text",ctx.getText());
226 return t;
228 @Override
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()));
233 return t;
235 @Override
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()));
241 return t;
244 @Override
245 public ST visitForBodyDo(mapleParserParser.ForBodyDoContext ctx) {
246 ST t = M2M.theTemplates.getInstanceOf("forbodydo");
247 t.add("statseq",visit(ctx.statseq()));
248 return t;
250 @Override
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());
256 return t;
258 @Override
259 public ST visitQuote(mapleParserParser.QuoteContext ctx) {
260 ST t = M2M.theTemplates.getInstanceOf("name");
261 t.add("text",ctx.getText());
262 return t;
264 @Override
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();
271 try{
272 d = new java.math.BigDecimal(s);
273 s = d.toString();
274 }catch(NumberFormatException e){};
275 t.add("text",s);
276 return t;
278 public ST visit1(String visitor, String op, mapleParserParser.ExprContext c)
280 ST v = null;
281 try{
282 v = visit(c);
283 } catch(Exception e){
284 say(visitor+" "+op+" "+e.toString());
286 if(v==null){
287 ST n = M2M.theTemplates.getInstanceOf("isnull");
288 n.add("name",visitor);
289 n.add("op",op);
290 return n;
292 return v;
294 public ST binexp(String exptype, String op, mapleParserParser.ExprContext l, mapleParserParser.ExprContext r)
296 ST t = M2M.theTemplates.getInstanceOf(exptype);
297 t.add("op",op);
298 t.add("l",visit1(exptype, op, l));
299 t.add("r",visit1(exptype, op, r));
300 return t;
302 @Override
303 public ST visitExpoOp(mapleParserParser.ExpoOpContext ctx) {
304 return binexp("binexp", ctx.OP.getText(),ctx.expr(0),ctx.expr(1));
306 @Override
307 public ST visitMultOp(mapleParserParser.MultOpContext ctx) {
308 return binexp("binexp", ctx.OP.getText(),ctx.expr(0),ctx.expr(1));
310 @Override
311 public ST visitAddOp(mapleParserParser.AddOpContext ctx) {
312 return binexp("binexp", ctx.OP.getText(),ctx.expr(0),ctx.expr(1));
314 @Override
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));
323 @Override
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());
329 return t;
331 @Override
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()));
340 //say(t.render());
341 return t;
343 @Override
344 public ST visitParenExpr(mapleParserParser.ParenExprContext ctx) {
345 ST t = M2M.theTemplates.getInstanceOf("paren");
346 t.add("e",visit(ctx.exprseq().expr(0)));
347 return t;
349 @Override
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();
356 try{
357 d = new java.math.BigInteger(s);
358 s = d.toString();
359 }catch(NumberFormatException e){};
360 t.add("text",s);
361 return t;
363 public void say(String s){ System.out.println(s); System.out.flush(); }