Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AST reflection #213

Merged
merged 9 commits into from
Jan 11, 2018
1 change: 1 addition & 0 deletions deep-rebuild
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fi
echo "Before we start, let's get this thing built ..."
java -Xss8M -Xmx2000M -jar ../jars/RunSilver.jar --clean silver:support:monto
java -Xss8M -Xmx2000M -jar ../jars/RunSilver.jar --clean lib:xml:ast
java -Xss8M -Xmx2000M -jar ../jars/RunSilver.jar --clean core:reflect
echo "Start ..."
time java -Xss8M -Xmx2000M -jar ../jars/RunSilver.jar --relative-jar --clean silver:composed:Default

Expand Down
86 changes: 86 additions & 0 deletions grammars/core/reflect/AST.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
grammar core:reflect;

imports silver:langutil;
imports silver:langutil:pp;

synthesized attribute pps :: [Document];

nonterminal AST with pp;

abstract production nonterminalAST
top::AST ::= prodName::String children::ASTs annotations::NamedASTs
{
top.pp = cat(text(prodName), parens(ppImplode(pp", ", children.pps ++ annotations.pps)));
}

abstract production listAST
top::AST ::= vals::ASTs
{
top.pp = brackets(ppImplode(pp", ", vals.pps));
}

abstract production stringAST
top::AST ::= s::String
{

}

abstract production integerAST
top::AST ::= i::Integer
{

}

abstract production floatAST
top::AST ::= f::Float
{

}

abstract production booleanAST
top::AST ::= b::Boolean
{

}

abstract production foreignAST
top::AST ::= x::a
{

}

nonterminal ASTs with pps;

abstract production consAST
top::ASTs ::= h::AST t::ASTs
{

}

abstract production nilAST
top::ASTs ::=
{

}

nonterminal NamedASTs with pps;

abstract production consNamedAST
top::NamedASTs ::= h::NamedAST t::NamedASTs
{

}

abstract production nilNamedAST
top::NamedASTs ::=
{

}

nonterminal NamedAST with pp;

abstract production namedAST
top::NamedAST ::= n::String v::AST
{

}
11 changes: 6 additions & 5 deletions grammars/silver/translation/java/core/Annotation.sv
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ top::AGDcl ::= 'annotation' a::QName tl::BracketedOptTypeExprs '::' te::TypeExpr
-- It should be fine, though. If we're a tv, then it's 'Object' and anything
-- else will be a subtype...

top.genFiles := [pair(className ++ ".java",
top.genFiles := [pair(className ++ ".java", s"""

"package " ++ makeName(top.grammarName) ++ ";\n\n" ++
package ${makeName(top.grammarName)};

"public interface " ++ className ++ " {\n\n" ++
public interface ${className} {

"\tpublic " ++ te.typerep.transType ++ " getAnno_" ++ makeIdName(fName) ++ "();\n\n" ++
public ${te.typerep.transType} getAnno_${makeIdName(fName)}();

"}\n")];
}
""")];
}

4 changes: 2 additions & 2 deletions grammars/silver/translation/java/core/AspectDcl.sv
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ aspect production aspectProductionDcl
top::AGDcl ::= 'aspect' 'production' id::QName ns::AspectProductionSignature body::ProductionBody
{
top.setupInh := body.setupInh;
top.initProd := "\t\t//ASPECT PRODUCTION " ++ id.name ++ " " ++ ns.pp ++ "\n" ++ body.translation;
top.initProd := s"\t\t//ASPECT PRODUCTION ${id.name} ${ns.pp}\n${body.translation}";
top.valueWeaving := body.valueWeaving;
}

aspect production aspectFunctionDcl
top::AGDcl ::= 'aspect' 'function' id::QName ns::AspectFunctionSignature body::ProductionBody
{
top.setupInh := body.setupInh;
top.initProd := "\t\t//ASPECT FUNCTION " ++ id.name ++ " " ++ ns.pp ++ "\n" ++ body.translation;
top.initProd := s"\t\t//ASPECT FUNCTION ${id.name} ${ns.pp}\n${body.translation}";
top.valueWeaving := body.valueWeaving;
}
16 changes: 8 additions & 8 deletions grammars/silver/translation/java/core/BuiltInFunctions.sv
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ top::Expr ::= e::Decorated Expr
aspect production stringLength
top::Expr ::= e::Decorated Expr
{
top.translation = "Integer.valueOf(((common.StringCatter)" ++ e.translation ++ ").length())";
top.translation = s"Integer.valueOf(((common.StringCatter)${e.translation}).length())";

top.lazyTranslation = wrapThunk(top.translation, top.frame.lazyApplication);
}
Expand All @@ -20,8 +20,8 @@ top::Expr ::= 'toInt' '(' e::Expr ')'
{
top.translation = case finalType(e) of
| intType() -> e.translation
| floatType() -> "Integer.valueOf(((Float)" ++ e.translation ++ ").intValue())"
| stringType() -> "Integer.valueOf(" ++ e.translation ++ ".toString())"
| floatType() -> s"Integer.valueOf(((Float)${e.translation}).intValue())"
| stringType() -> s"Integer.valueOf(${e.translation}.toString())"
| t -> error("INTERNAL ERROR: no toInt translation for type " ++ prettyType(t))
end;

Expand All @@ -31,9 +31,9 @@ aspect production toFloatFunction
top::Expr ::= 'toFloat' '(' e::Expr ')'
{
top.translation = case finalType(e) of
| intType() -> "Float.valueOf(((Integer)" ++ e.translation ++ ").floatValue())"
| intType() -> s"Float.valueOf(((Integer)${e.translation}).floatValue())"
| floatType() -> e.translation
| stringType() -> "Float.valueOf(" ++ e.translation ++ ".toString())"
| stringType() -> s"Float.valueOf(${e.translation}.toString())"
| t -> error("INTERNAL ERROR: no toFloat translation for type " ++ prettyType(t))
end;

Expand All @@ -42,23 +42,23 @@ top::Expr ::= 'toFloat' '(' e::Expr ')'
aspect production toStringFunction
top::Expr ::= 'toString' '(' e::Expr ')'
{
top.translation = "new common.StringCatter(String.valueOf(" ++ e.translation ++ "))";
top.translation = s"new common.StringCatter(String.valueOf(${e.translation}))";

top.lazyTranslation = wrapThunk(top.translation, top.frame.lazyApplication);
}

aspect production newFunction
top::Expr ::= 'new' '(' e::Expr ')'
{
top.translation = "((" ++ finalType(top).transType ++ ")" ++ e.translation ++ ".undecorate())";
top.translation = s"((${finalType(top).transType})${e.translation}.undecorate())";

top.lazyTranslation = wrapThunk(top.translation, top.frame.lazyApplication);
}

aspect production terminalConstructor
top::Expr ::= 'terminal' '(' t::TypeExpr ',' es::Expr ',' el::Expr ')'
{
top.translation = "new " ++ makeTerminalName(t.typerep.typeName) ++ "(" ++ es.translation ++ ", (core.NLocation)" ++ el.translation ++ ")";
top.translation = s"new ${makeTerminalName(t.typerep.typeName)}(${es.translation}, (core.NLocation)${el.translation})";

top.lazyTranslation = wrapThunk(top.translation, top.frame.lazyApplication);
}
Loading