Skip to content

Commit

Permalink
Added for loop statements and error checking to the DSL.
Browse files Browse the repository at this point in the history
Added lua code generation for for loops.
Improved lua pretty printing for while and for loops.
  • Loading branch information
Gohla committed Jul 22, 2011
1 parent 07863ad commit 0f71c06
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions DSL/DiversiaScript/syntax/Statement.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ exports
"if" "(" Exp ")" Statement "else" Statement -> Statement {cons("If")}
"if" "(" Exp ")" Statement -> Statement {cons("If")}
"while" "(" Exp ")" Statement -> Statement {cons("While")}
"for" "(" {VarDef ","}* ";" Exp ";" Statement ")" Statement -> Statement {cons("For")}
"for" "(" VarDef "in" Exp ")" Statement -> Statement {cons("ForArray")}
"for" "(" VarDef "," VarDef "in" Exp ")" Statement -> Statement {cons("ForDict")}
Exp "=" Exp ";" -> Statement {cons("Assign")}
"setstate" StateRef ";" -> Statement {cons("StateChange")}
"switch" "(" Exp ")" "{" Case* "}" -> Statement {cons("Switch")}
Expand Down
5 changes: 5 additions & 0 deletions DSL/DiversiaScript/trans/codegen/lua/generate.str
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ rules // Statements
to-lua: If(condition, statements) -> If(<to-lua> condition, Chunk([<to-lua> statements]))
to-lua: If(condition, trueStatements, falseStatements) -> If(<to-lua> condition, Chunk([<to-lua> trueStatements]), Chunk([<to-lua> falseStatements]))
to-lua: While(condition, statements) -> While(<to-lua> condition, Chunk(<map(to-lua)> statements))
to-lua: ForArray(VarDef(_, _{name}, _, _), array, statement) ->
ForIn(["__dummy", name], [Call(VarRef("ipairs"), Args([<to-lua> array]))], Chunk([<to-lua> statement]))
to-lua: ForDict(VarDef(_, _{keyName}, _, _), VarDef(_, _{valName}, _, _), dict, statement) ->
ForIn([keyName, valName], [Call(VarRef("pairs"), Args([<to-lua> dict]))], Chunk([<to-lua> statement]))
to-lua: For(varDefs, condition, incrementer, statement) -> [<map(to-lua)> varDefs, While(<to-lua> condition, Chunk([<to-lua> statement, <to-lua> incrementer]))]
to-lua: Assign(exp, val) -> Assignment([<to-lua> exp], [<to-lua> val])
where not (<?ObjectNew(_, _)> val)
where not (<match-type> (<type-of> exp, Object()))
Expand Down
33 changes: 33 additions & 0 deletions DSL/DiversiaScript/trans/editor/check/error.str
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,39 @@ rules // Statements
<type-of> exp => type;
not (<match-type> (StringType(), type))

editor-error: ForArray(varDef, array, _) -> (varDef, $[Expected [<printable> valType] but found [<printable> varType].])
where
<type-of> varDef => varType;
<type-of> array => ArrayType(valType);
not (<match-type> (varType, valType))

editor-error: ForArray(_, array, _) -> (array, $[Expected Array but found [<printable> arrayType].])
where
<type-of> array => arrayType;
not (<?ArrayType(_)> arrayType)

editor-error: ForDict(keyVarDef, _, dict, _) -> (keyVarDef, $[Expected [<printable> keyType] but found [<printable> keyVarType].])
where
<type-of> keyVarDef => keyVarType;
<type-of> dict => DictType(keyType, _);
not (<match-type> (keyVarType, keyType))

editor-error: ForDict(_, valVarDef, dict, _) -> (valVarDef, $[Expected [<printable> valType] but found [<printable> valVarType].])
where
<type-of> valVarDef => valVarType;
<type-of> dict => DictType(_, valType);
not (<match-type> (valVarType, valType))

editor-error: ForDict(_, _, dict, _) -> (dict, $[Expected Dict but found [<printable> dictType].])
where
<type-of> dict => dictType;
not (<?DictType(_, _)> dictType)

editor-error: For(_, exp, _, _) -> (exp, $[Expected Boolean (convertable) type but found [<printable> type].])
where
<type-of> exp => type;
not (<bool-convertable> type)

rules // Debug

editor-error: e@ComponentProp(name, exp) -> (e, $[Decorate failed])
Expand Down
10 changes: 5 additions & 5 deletions DSL/Lua/syntax/Lua.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
Assignment -- H hs=1[_1 KW["="] _2],
Assignment.1:iter-sep -- _1 KW[", "],
Assignment.2:iter-sep -- _1 KW[", "],
DoWhile -- KW["do"] _1 KW["end"],
While -- KW["while"] _1 KW["do"] _2 KW["end"],
DoWhile -- V is=2[KW["do"] _1] KW["end"],
While -- V is=2[H hs=0[KW["while "] _1 KW[" do "]] _2] KW["end"],
Repeat -- KW["repeat"] _1 KW["until"] _2,
ElseIfPart -- V is=2[H[KW["elseif"] _1 KW["then"]] _2],
If -- V is=2[H[KW["if"] _1 KW["then"]] _2] KW["end"],
Expand All @@ -19,9 +19,9 @@
ExpPart -- KW[","] _1,
For -- KW["for"] _1 KW["="] _2 KW[","] _3 _4 KW["do"] _5 KW["end"],
For.4:opt -- _1,
ForIn -- KW["for"] _1 KW["in"] _2 KW["do"] _3 KW["end"],
ForIn.1:iter-sep -- _1 KW[","],
ForIn.2:iter-sep -- _1 KW[","],
ForIn -- V is=2[H hs=0[KW["for "] _1 KW[" in "] _2 KW[" do"]] _3] KW["end"],
ForIn.1:iter-sep -- H hs=0[_1 KW[", "]],
ForIn.2:iter-sep -- H hs=0[_1 KW[", "]],
FuncDef -- V is=2[H[KW["function"] H hs=0[_1 KW["("] _2 KW[")"]]] _3] KW["end"],
LocalFuncDef -- KW["local"] KW["function"] _1 KW["("] _2 KW[")"] _3 KW["end"],
LocalVarDef -- V [H [KW["local"]] _1],
Expand Down

0 comments on commit 0f71c06

Please sign in to comment.