package sizzle.compiler;
import sizzle.parser.syntaxtree.ArrayType;
import sizzle.parser.syntaxtree.Assignment;
import sizzle.parser.syntaxtree.Block;
import sizzle.parser.syntaxtree.BreakStatement;
import sizzle.parser.syntaxtree.BytesLiteral;
import sizzle.parser.syntaxtree.Call;
import sizzle.parser.syntaxtree.CharLiteral;
import sizzle.parser.syntaxtree.Comparison;
import sizzle.parser.syntaxtree.Component;
import sizzle.parser.syntaxtree.Composite;
import sizzle.parser.syntaxtree.Conjunction;
import sizzle.parser.syntaxtree.ContinueStatement;
import sizzle.parser.syntaxtree.Declaration;
import sizzle.parser.syntaxtree.DoStatement;
import sizzle.parser.syntaxtree.EmitStatement;
import sizzle.parser.syntaxtree.ExprList;
import sizzle.parser.syntaxtree.ExprStatement;
import sizzle.parser.syntaxtree.Expression;
import sizzle.parser.syntaxtree.Factor;
import sizzle.parser.syntaxtree.FingerprintLiteral;
import sizzle.parser.syntaxtree.FloatingPointLiteral;
import sizzle.parser.syntaxtree.ForExprStatement;
import sizzle.parser.syntaxtree.ForStatement;
import sizzle.parser.syntaxtree.ForVarDecl;
import sizzle.parser.syntaxtree.Function;
import sizzle.parser.syntaxtree.FunctionType;
import sizzle.parser.syntaxtree.Identifier;
import sizzle.parser.syntaxtree.IdentifierList;
import sizzle.parser.syntaxtree.IfStatement;
import sizzle.parser.syntaxtree.Index;
import sizzle.parser.syntaxtree.IntegerLiteral;
import sizzle.parser.syntaxtree.MapType;
import sizzle.parser.syntaxtree.Node;
import sizzle.parser.syntaxtree.NodeChoice;
import sizzle.parser.syntaxtree.NodeSequence;
import sizzle.parser.syntaxtree.Operand;
import sizzle.parser.syntaxtree.OutputType;
import sizzle.parser.syntaxtree.Pair;
import sizzle.parser.syntaxtree.PairList;
import sizzle.parser.syntaxtree.Program;
import sizzle.parser.syntaxtree.Proto;
import sizzle.parser.syntaxtree.ProtoFieldDecl;
import sizzle.parser.syntaxtree.ProtoMember;
import sizzle.parser.syntaxtree.ProtoMemberList;
import sizzle.parser.syntaxtree.ProtoTupleType;
import sizzle.parser.syntaxtree.Regexp;
import sizzle.parser.syntaxtree.RegexpList;
import sizzle.parser.syntaxtree.ResultStatement;
import sizzle.parser.syntaxtree.ReturnStatement;
import sizzle.parser.syntaxtree.Selector;
import sizzle.parser.syntaxtree.SimpleExpr;
import sizzle.parser.syntaxtree.SimpleMember;
import sizzle.parser.syntaxtree.SimpleMemberList;
import sizzle.parser.syntaxtree.SimpleTupleType;
import sizzle.parser.syntaxtree.Start;
import sizzle.parser.syntaxtree.Statement;
import sizzle.parser.syntaxtree.StatementExpr;
import sizzle.parser.syntaxtree.StaticVarDecl;
import sizzle.parser.syntaxtree.StringLiteral;
import sizzle.parser.syntaxtree.SwitchStatement;
import sizzle.parser.syntaxtree.Term;
import sizzle.parser.syntaxtree.TimeLiteral;
import sizzle.parser.syntaxtree.TupleType;
import sizzle.parser.syntaxtree.Type;
import sizzle.parser.syntaxtree.TypeDecl;
import sizzle.parser.syntaxtree.VarDecl;
import sizzle.parser.syntaxtree.WhenStatement;
import sizzle.parser.syntaxtree.WhileStatement;
import sizzle.parser.visitor.GJDepthFirst;
public class StaticInitializationCodeGeneratingVisitor extends GJDepthFirst<String, SymbolTable> {
private final CodeGeneratingVisitor codegenerator;
public StaticInitializationCodeGeneratingVisitor(final CodeGeneratingVisitor codegenerator) {
this.codegenerator = codegenerator;
}
@Override
public String visit(final Start n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Program n, final SymbolTable argu) {
final StringBuilder sb = new StringBuilder();
for (final Node node : n.f0.nodes) {
final NodeChoice nodeChoice = (NodeChoice) node;
switch (nodeChoice.which) {
case 0: // declaration
final String accept = nodeChoice.choice.accept(this, argu);
if (accept != null)
sb.append(accept);
break;
case 1: // statement
break;
case 2: // proto
default:
throw new RuntimeException("unexpected choice " + nodeChoice.which + " is " + nodeChoice.choice.getClass());
}
}
return sb.toString();
}
@Override
public String visit(final Declaration n, final SymbolTable argu) {
switch (n.f0.which) {
case 0: // type declaration
return null;
case 1: // static var declaration
return n.f0.choice.accept(this, argu);
case 2: // variable declaration
return null;
default:
throw new RuntimeException("unexpected choice " + n.f0.which + " is " + n.f0.choice.getClass());
}
}
@Override
public String visit(final TypeDecl n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final StaticVarDecl n, final SymbolTable argu) {
return n.f1.accept(this, argu);
}
@Override
public String visit(final VarDecl n, final SymbolTable argu) {
if (n.f3.present()) {
final NodeChoice nodeChoice = (NodeChoice) n.f3.node;
switch (nodeChoice.which) {
case 0: // initializer
argu.setId("___" + n.f0.f0.tokenImage);
final String accept = ((NodeSequence) nodeChoice.choice).elementAt(1).accept(this, argu);
argu.setId(null);
return accept;
default:
throw new RuntimeException("unexpected choice " + nodeChoice.which + " is " + nodeChoice.choice.getClass());
}
} else {
return null;
}
}
@Override
public String visit(final Type n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Component n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ArrayType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final TupleType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final SimpleTupleType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final SimpleMemberList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final SimpleMember n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ProtoTupleType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ProtoMemberList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ProtoMember n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ProtoFieldDecl n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final MapType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final OutputType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ExprList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final FunctionType n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Statement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Assignment n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Block n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final BreakStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ContinueStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final DoStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final EmitStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ExprStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ForStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ForVarDecl n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ForExprStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final IfStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ResultStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final ReturnStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final SwitchStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final WhenStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final IdentifierList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final WhileStatement n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Expression n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Conjunction n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Comparison n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final SimpleExpr n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Term n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Factor n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Selector n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Index n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Call n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final RegexpList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Regexp n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Operand n, final SymbolTable argu) {
return n.f0.accept(this, argu);
}
@Override
public String visit(final Composite n, final SymbolTable argu) {
if (n.f1.present()) {
final NodeChoice nodeChoice = (NodeChoice) n.f1.node;
switch (nodeChoice.which) {
case 0: // pair list
return this.codegenerator.visit((PairList) nodeChoice.choice, argu);
case 1: // expression list
case 2: // empty map
return null;
default:
throw new RuntimeException("unexpected choice " + nodeChoice.which + " is " + nodeChoice.choice.getClass());
}
} else {
throw new RuntimeException("unimplemented");
}
}
@Override
public String visit(final PairList n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Pair n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Function n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final StatementExpr n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Proto n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final Identifier n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final IntegerLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final FingerprintLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final FloatingPointLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final CharLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final StringLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final BytesLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
@Override
public String visit(final TimeLiteral n, final SymbolTable argu) {
throw new RuntimeException("unimplemented");
}
}