diff --git a/DSL/DiversiaScript/syntax/Statement.sdf b/DSL/DiversiaScript/syntax/Statement.sdf index aa499e9..6e6ffb9 100644 --- a/DSL/DiversiaScript/syntax/Statement.sdf +++ b/DSL/DiversiaScript/syntax/Statement.sdf @@ -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")} diff --git a/DSL/DiversiaScript/trans/codegen/lua/generate.str b/DSL/DiversiaScript/trans/codegen/lua/generate.str index bf565c1..2221c51 100644 --- a/DSL/DiversiaScript/trans/codegen/lua/generate.str +++ b/DSL/DiversiaScript/trans/codegen/lua/generate.str @@ -332,6 +332,11 @@ rules // Statements to-lua: If(condition, statements) -> If( condition, Chunk([ statements])) to-lua: If(condition, trueStatements, falseStatements) -> If( condition, Chunk([ trueStatements]), Chunk([ falseStatements])) to-lua: While(condition, statements) -> While( condition, Chunk( statements)) + to-lua: ForArray(VarDef(_, _{name}, _, _), array, statement) -> + ForIn(["__dummy", name], [Call(VarRef("ipairs"), Args([ array]))], Chunk([ statement])) + to-lua: ForDict(VarDef(_, _{keyName}, _, _), VarDef(_, _{valName}, _, _), dict, statement) -> + ForIn([keyName, valName], [Call(VarRef("pairs"), Args([ dict]))], Chunk([ statement])) + to-lua: For(varDefs, condition, incrementer, statement) -> [ varDefs, While( condition, Chunk([ statement, incrementer]))] to-lua: Assign(exp, val) -> Assignment([ exp], [ val]) where not ( val) where not ( ( exp, Object())) diff --git a/DSL/DiversiaScript/trans/editor/check/error.str b/DSL/DiversiaScript/trans/editor/check/error.str index 50ff29b..868c75d 100644 --- a/DSL/DiversiaScript/trans/editor/check/error.str +++ b/DSL/DiversiaScript/trans/editor/check/error.str @@ -294,6 +294,39 @@ rules // Statements exp => type; not ( (StringType(), type)) + editor-error: ForArray(varDef, array, _) -> (varDef, $[Expected [ valType] but found [ varType].]) + where + varDef => varType; + array => ArrayType(valType); + not ( (varType, valType)) + + editor-error: ForArray(_, array, _) -> (array, $[Expected Array but found [ arrayType].]) + where + array => arrayType; + not ( arrayType) + + editor-error: ForDict(keyVarDef, _, dict, _) -> (keyVarDef, $[Expected [ keyType] but found [ keyVarType].]) + where + keyVarDef => keyVarType; + dict => DictType(keyType, _); + not ( (keyVarType, keyType)) + + editor-error: ForDict(_, valVarDef, dict, _) -> (valVarDef, $[Expected [ valType] but found [ valVarType].]) + where + valVarDef => valVarType; + dict => DictType(_, valType); + not ( (valVarType, valType)) + + editor-error: ForDict(_, _, dict, _) -> (dict, $[Expected Dict but found [ dictType].]) + where + dict => dictType; + not ( dictType) + + editor-error: For(_, exp, _, _) -> (exp, $[Expected Boolean (convertable) type but found [ type].]) + where + exp => type; + not ( type) + rules // Debug editor-error: e@ComponentProp(name, exp) -> (e, $[Decorate failed]) diff --git a/DSL/Lua/syntax/Lua.pp b/DSL/Lua/syntax/Lua.pp index c8ec9ad..7a97569 100644 --- a/DSL/Lua/syntax/Lua.pp +++ b/DSL/Lua/syntax/Lua.pp @@ -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"], @@ -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],