Commit b3a45cd4 authored by Anders Nilsson's avatar Anders Nilsson
Browse files

Imported source

parents
aspect ArrayRewrite {
syn boolean Dim.isVariable() {
for (int i = 0 ; i < getNumExp() ; i++) {
if (getExp(i) instanceof VariableSize) {
return true;
}
}
return false;
}
rewrite ParseArrayType {
when (! getDim(0).isVariable())
to FixedArrayType {
if (getNumDim() == 1) {
return new FixedArrayType(getType(),
getDim(0).getExpList());
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new FixedArrayType(new ParseArrayType(getType(), l),
getDim(0).getExpList());
}
}
when (getDim(0).isVariable())
to VariableArrayType {
if (getNumDim() == 1) {
return new VariableArrayType(getType(),
getDim(0).getExpList());
} else {
List l = new List();
for (int i = 1 ; i < getNumDim() ; i++) {
l.add(getDim(i));
}
return new VariableArrayType(new ParseArrayType(getType(), l),
getDim(0).getExpList());
}
}
}
}
This diff is collapsed.
This diff is collapsed.
import java.util.Collection;
aspect ErrorCheck {
syn int ASTNode.lineNumber() = getLine(getStart());
protected String ASTNode.errors = null;
protected void ASTNode.error(String s) {
s = "Error at " + lineNumber() + ": " + s;
if(errors == null) {
errors = s;
} else {
errors = errors + "\n" + s;
}
}
protected boolean ASTNode.hasErrors() {
return errors != null;
}
public void ASTNode.errorCheck(Collection collection) {
nameCheck();
if(hasErrors())
collection.add(errors);
for(int i = 0; i < getNumChild(); i++) {
getChild(i).errorCheck(collection);
}
}
}
This diff is collapsed.
Program ::= Decl*;
abstract Decl ::= Type <Name:String>;
TypeDecl : Decl;
SampleDecl : Decl;
Field ::= Type <Name:String>;
abstract Type;
VoidType : Type;
PrimType : Type ::= <Name:String> <Token:int>;
UserType : Type ::= <Name:String>;
StructType : Type ::= Field*;
ParseArrayType : Type ::= Type Dim*;
abstract ArrayType : Type ::= Type Exp*;
VariableArrayType : ArrayType;
FixedArrayType : ArrayType;
Dim ::= Exp*;
abstract Exp;
IntegerLiteral : Exp ::= <Value:String>;
VariableSize : Exp;
import AST.*;
import java.io.*;
import java.util.*;
public class LabComm {
private static void println(String s) {
System.out.println(s);
}
private static void print_help() {
println("\n Usage: java -jar labcom.jar [options*] FILE");
println("");
println(" --help Shows this help text");
println(" -v Be verbose");
println("[ C options ]");
println(" -C Generates C/H code in FILE.[ch]");
println(" --cprefix=PREFIX Prefixes C types with PREFIX");
println(" --cinclude=FILE Include FILE in generated .c");
println(" --c=CFILE Generates C code in CFILE");
println(" --h=HFILE Generates H code in HFILE");
println("[ C# options]");
println(" --cs Generates C# code in FILE.cs");
println(" --cs=CSFILE Generates C# code in CSFILE");
println(" --csnamespace=NAMESPACE Place C# classes in NAMESPACE");
println("[ Java options ]");
println(" --java=JDIR Generates Java files in JDIR");
println(" --javapackage=PACKAGE Place Java classes in PACKAGE");
println("[ Python options ]");
println(" -P Generates Python code in FILE.py");
println(" --python=PFILE Generates Python code in PFILE");
println("[ Misc options ]");
println(" --pretty Pretty prints on standard output");
println(" --typeinfo=TIFILE Generates typeinfo in TIFILE");
}
private static String getCoreName(String s) {
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getFileName(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
private static String getBaseName(String s) {
s = getFileName(s);
int i = s.lastIndexOf('.');
return s.substring(0, i > 0 ? i : s.length());
}
private static String getPrefix(String s) {
return s.substring(s.lastIndexOf('/') + 1, s.length());
}
public static void main(String[] args) {
String fileName = null;
// Scan for first non-option
for (int i = 0 ; i < args.length ; i++) {
if (! args[i].startsWith("-")) {
fileName = args[i];
break;
}
}
String coreName = null;
String prefix = null;
if (fileName != null) {
coreName = getBaseName(fileName);
prefix = getPrefix(coreName);
}
boolean verbose = false;
String cFile = null;
String hFile = null;
Vector cIncludes = new Vector();
String cPrefix = prefix;
String csFile = null;
String csNamespace = null;
String javaDir = null;
String javaPackage = "";
String pythonFile = null;
String prettyFile = null;
String typeinfoFile = null;
for (int i = 0 ; i < args.length ; i++) {
if (fileName == null ||
args[i].equals("-help") ||
args[i].equals("-h") ||
args[i].equals("--help")) {
print_help();
System.exit(0);
} else if (args[i].equals("-v")) {
verbose=true;
} else if (args[i].equals("-C")) {
cFile = coreName + ".c";
hFile = coreName + ".h";
} else if (args[i].startsWith("--cinclude=")) {
cIncludes.add(args[i].substring(11));
} else if (args[i].startsWith("--cprefix=")) {
cPrefix = args[i].substring(10);
} else if (args[i].startsWith("--c=")) {
cFile = args[i].substring(4);
} else if (args[i].startsWith("--h=")) {
hFile = args[i].substring(4);
} else if (args[i].equals("--cs")) {
csFile = coreName + ".cs";
} else if (args[i].startsWith("--cs=")) {
csFile = args[i].substring(5);
} else if (args[i].startsWith("--csnamespace=")) {
csNamespace = args[i].substring(14);
} else if (args[i].startsWith("--java=")) {
javaDir = args[i].substring(7);
} else if (args[i].startsWith("--javapackage=")) {
javaPackage = args[i].substring(14);
} else if (args[i].equals("-P")) {
pythonFile = coreName + ".py";
} else if (args[i].startsWith("--python=")) {
pythonFile = args[i].substring(9);
} else if (args[i].startsWith("--pretty=")) {
prettyFile = args[i].substring(9);
} else if (args[i].startsWith("--typeinfo=")) {
typeinfoFile = args[i].substring(11);
} else if (i == args.length - 1) {
fileName = args[i];
} else {
System.err.println(" Unknown argument " + args[i]);
print_help();
System.exit(2);
}
}
if (fileName == null) {
print_help();
System.exit(1);
} else {
Program ast = null;
try {
// Check for errors
LabCommScanner scanner = new LabCommScanner(new FileReader(fileName));
LabCommParser parser = new LabCommParser();
Program p = (Program)parser.parse(scanner);
Collection errors = new LinkedList();
p.errorCheck(errors);
if (errors.isEmpty()) {
ast = p;
} else {
for (Iterator iter = errors.iterator(); iter.hasNext(); ) {
String s = (String)iter.next();
System.out.println(s);
}
}
} catch (FileNotFoundException e) {
System.err.println("Could not find file: " + fileName);
} catch (IOException e) {
System.err.println("IOException: " + fileName + " " + e);
} catch (beaver.Parser.Exception e) {
System.err.println(e.getMessage());
}
if (ast != null) {
Vector hIncludes = new Vector(cIncludes);
if (hFile != null) {
cIncludes.add(hFile);
}
boolean prettyOnStdout = true;
if (cFile != null) {
if (verbose) { System.err.println("Generating C: " + cFile); }
genC(ast, cFile, cIncludes, coreName, cPrefix);
prettyOnStdout = false;
}
if (hFile != null) {
if (verbose) { System.err.println("Generating H: " + hFile); }
genH(ast, hFile, hIncludes, coreName, cPrefix);
prettyOnStdout = false;
}
if (csFile != null) {
if (verbose) { System.err.println("Generating C#: " + csFile); }
genCS(ast, csFile, csNamespace);
prettyOnStdout = false;
}
if (javaDir != null) {
if (verbose) { System.err.println("Generating Java: " + javaDir); }
genJava(ast, javaDir, javaPackage);
prettyOnStdout = false;
}
if (pythonFile != null) {
if (verbose) {
System.err.println("Generating Python: " + pythonFile);
}
genPython(ast, pythonFile, prefix);
prettyOnStdout = false;
}
if (prettyFile != null) {
if (verbose) {
System.err.println("Generating Pretty: " + prettyFile);
}
try {
FileOutputStream f = new FileOutputStream(prettyFile);
PrintStream out = new PrintStream(f);
ast.pp(out);
out.close();
prettyOnStdout = false;
} catch (IOException e) {
System.err.println("IOException: " + prettyFile + " " + e);
}
}
if (typeinfoFile != null) {
if (verbose) {
System.err.println("Generating TypeInfo: " + typeinfoFile);
}
try {
FileOutputStream f = new FileOutputStream(typeinfoFile);
PrintStream out = new PrintStream(f);
ast.C_info(out, cPrefix);
ast.Java_info(out);
ast.CS_info(out, csNamespace);
prettyOnStdout = false;
} catch (IOException e) {
System.err.println("IOException: " + typeinfoFile + " " + e);
}
}
if (prettyOnStdout) {
ast.pp(System.out);
}
}
}
}
private static void genH(Program p, String hName,
Vector cIncludes, String coreName, String prefix) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(hName);
out = new PrintStream(f);
p.C_genH(out, cIncludes, coreName, prefix);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + hName + " " + e);
}
}
private static void genC(Program p, String cName,
Vector cIncludes, String coreName, String prefix) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(cName);
out = new PrintStream(f);
p.C_genC(out, cIncludes, coreName, prefix);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + cName + " " + e);
}
}
private static void genCS(Program p, String csName, String csNamespace) {
try {
p.CS_gen(csName, csNamespace);
} catch (IOException e) {
System.err.println("IOException: " + csName + " " +
csNamespace + " " + e);
}
}
private static void genJava(Program p, String dirName, String packageName) {
try {
p.J_gen(dirName, packageName);
} catch (IOException e) {
System.err.println("IOException: " + dirName + " " +
packageName + " " + e);
}
}
private static void genPython(Program p, String filename, String prefix) {
try {
FileOutputStream f;
PrintStream out;
f = new FileOutputStream(filename);
out = new PrintStream(f);
p.Python_gen(out, prefix);
out.close();
} catch (IOException e) {
System.err.println("IOException: " + filename + " " + e);
}
}
}
%header {:
package AST;
:};
%embed {:
public static class SourceError extends Error {
public SourceError(String msg) {
super(msg);
}
}
class Events extends Parser.Events {
public void syntaxError(Symbol token) {
StringBuffer s = new StringBuffer();
s.append(token.getLine(token.getStart()) + ", " + token.getColumn(token.getStart()) + "\n");
s.append(" *** Syntactic error: unexpected token " + Terminals.NAMES[token.getId()]);
throw new SourceError(s.toString());
//super.syntaxError(token);
//throw new RuntimeException(token.getLine(token.getStart()) + ", " +
// token.getColumn(token.getStart()) + ": Syntax Error");
}
public void scannerError(Scanner.Exception e) {
StringBuffer s = new StringBuffer();
s.append(e.line + ", " + e.column + "\n");
s.append(" *** Lexical error: " + e.getMessage());
throw new SourceError(s.toString());
//super.scannerError(e);
//throw new RuntimeException("Unexpected token");
}
}
{
report = new Events(); // Use error handler in parser
}
:};
Program goal =
/* Empty program */ {: return new Program(); :}
| decl_list.l {: return new Program(l); :}
;
List decl_list =
decl.d {: return new List().add(d); :}
| decl_list.l decl.d {: return l.add(d); :}
;
Decl decl =
type_decl.t {: return t; :}
| sample_decl.s {: return s; :}
;
List var_decl_list =
var_decl.v {: return new List().add(v); :}
| var_decl_list.l var_decl.v {: return l.add(v); :}
;
Field var_decl =
type.t IDENTIFIER SEMICOLON {: return new Field(t, IDENTIFIER); :}
| type.t IDENTIFIER dim_list.d SEMICOLON
{: return new Field(new ParseArrayType(t, d), IDENTIFIER); :}
;
TypeDecl type_decl =
TYPEDEF type.t IDENTIFIER SEMICOLON {: return new TypeDecl(t, IDENTIFIER); :}
| TYPEDEF type.t IDENTIFIER dim_list.d SEMICOLON
{: return new TypeDecl(new ParseArrayType(t, d), IDENTIFIER); :}
;
SampleDecl sample_decl =
SAMPLE type.t IDENTIFIER SEMICOLON
{: return new SampleDecl(t, IDENTIFIER); :}
| SAMPLE type.t IDENTIFIER dim_list.d SEMICOLON
{: return new SampleDecl(new ParseArrayType(t, d), IDENTIFIER); :}
| SAMPLE VOID IDENTIFIER SEMICOLON
{: return new SampleDecl(new VoidType(), IDENTIFIER); :}
;
Type type =
prim_type.p {: return p; :}
| user_type.u {: return u; :}
| struct_type.s {: return s; :}
;
PrimType prim_type =
BOOLEAN
{: return new PrimType(BOOLEAN, ASTNode.LABCOMM_BOOLEAN); :}
| BYTE
{: return new PrimType(BYTE, ASTNode.LABCOMM_BYTE); :}
| SHORT
{: return new PrimType(SHORT, ASTNode.LABCOMM_SHORT); :}
| INT
{: return new PrimType(INT, ASTNode.LABCOMM_INT); :}
| LONG
{: return new PrimType(LONG, ASTNode.LABCOMM_LONG); :}
| FLOAT
{: return new PrimType(FLOAT, ASTNode.LABCOMM_FLOAT); :}
| DOUBLE
{: return new PrimType(DOUBLE, ASTNode.LABCOMM_DOUBLE); :}
| STRING
{: return new PrimType(STRING, ASTNode.LABCOMM_STRING); :}
;
UserType user_type =
IDENTIFIER {: return new UserType(IDENTIFIER); :}
;
StructType struct_type =
STRUCT LBRACE var_decl_list.l RBRACE {: return new StructType(l); :}
;
List dim_list =
dim.d {: return new List().add(d); :}
| dim_list.l dim.d {: return l.add(d); :}
;
Dim dim =
LBRACK exp_list.e RBRACK {: return new Dim(e); :}
;
List exp_list =
exp.e {: return new List().add(e); :}
| exp_list.l COMMA exp.e {: return l.add(e); :}
;
Exp exp =
INTEGER_LITERAL {: return new IntegerLiteral(INTEGER_LITERAL); :}
| UNDERSCORE {: return new VariableSize(); :}
;
package AST;
import beaver.Symbol;
import beaver.Scanner;
import AST.LabCommParser.Terminals;
%%
%public
%final
%class LabCommScanner
%extends Scanner
%type Symbol
%function nextToken
%yylexthrow Scanner.Exception
%unicode
%line %column
%{
StringBuffer strbuf = new StringBuffer(128);
private Symbol sym(short id) {
return new Symbol(id, yyline + 1, yycolumn + 1, len(), str());
}
private Symbol sym(short id, String value) {
return new Symbol(id, yyline + 1, yycolumn + 1, len(), value);
}
private String str() { return yytext(); }
private int len() { return yylength(); }
%}
LineTerminator = \n|\r|\r\n
InputCharacter = [^\r\n]
WhiteSpace = [ ] | \t | \f | {LineTerminator}
Comment = {TraditionalComment}
| {EndOfLineComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" | "/*" "*"+ [^/*] ~"*/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
Identifier = [:jletter:][:jletterdigit:]*
DecimalNumeral = 0 | {NonZeroDigit} {Digits}?
Digits = {Digit}+
Digit = 0 | {NonZeroDigit}
NonZeroDigit = [1-9]
%%
<YYINITIAL> {
{WhiteSpace} { }
{Comment} { }
"sample" { return sym(Terminals.SAMPLE); }
"typedef" { return sym(Terminals.TYPEDEF); }
"struct" { return sym(Terminals.STRUCT); }
"void" { return sym(Terminals.VOID); }
"boolean" { return sym(Terminals.BOOLEAN); }
"byte" { return sym(Terminals.BYTE); }
"short" { return sym(Terminals.SHORT); }
"int" { return sym(Terminals.INT); }
"long" { return sym(Terminals.LONG); }
"float" { return sym(Terminals.FLOAT); }
"double" { return sym(Terminals.DOUBLE); }
"string" { return sym(Terminals.STRING); }
{DecimalNumeral} { return sym(Terminals.INTEGER_LITERAL); }
"_" { return sym(Terminals.UNDERSCORE); }
"{" { return sym(Terminals.LBRACE); }
"}" { return sym(Terminals.RBRACE); }
"[" { return sym(Terminals.LBRACK); }
"]" { return sym(Terminals.RBRACK); }
";" { return sym(Terminals.SEMICOLON); }
"," { return sym(Terminals.COMMA); }
{Identifier} { return sym(Terminals.IDENTIFIER); }
}
// fall through errors
.|\n { throw new RuntimeException("Illegal character \""+str()+ "\" at line "+yyline+", column "+yycolumn); }