From 07863adfada0193e1b17a42c405834edcdebafa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabri=C3=ABl=20Konat?= Date: Fri, 22 Jul 2011 13:48:02 +0200 Subject: [PATCH] Added array/dictionary types, literals and error checking. Added lua code generation for array/dictionary literals. Improved lua pretty printing for tables. --- DSL/DiversiaScript/syntax/Expression.sdf | 2 ++ DSL/DiversiaScript/syntax/Type.sdf | 2 ++ .../trans/codegen/lua/generate.str | 2 ++ .../trans/editor/check/error.str | 14 ++++++++++- DSL/DiversiaScript/trans/projection/type.str | 23 +++++++++++++++---- DSL/DiversiaScript/trans/util.str | 1 + DSL/Lua/syntax/Lua.pp | 18 +++++++-------- 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/DSL/DiversiaScript/syntax/Expression.sdf b/DSL/DiversiaScript/syntax/Expression.sdf index c45a9e3..e6a244d 100644 --- a/DSL/DiversiaScript/syntax/Expression.sdf +++ b/DSL/DiversiaScript/syntax/Expression.sdf @@ -22,6 +22,8 @@ exports "[" Exp "," Exp "]" -> Exp {cons("Vector2Const")} "[" Exp "," Exp "," Exp "]" -> Exp {cons("Vector3Const")} "[" Exp "," Exp "," Exp "," Exp "]" -> Exp {cons("Vector4Const")} + "{" {Exp ","}+ "}" -> Exp {cons("ArrayConst")} + "{" {(Exp "=" Exp) ","}+ "}" -> Exp {cons("DictConst")} Id -> Exp {cons("VarRef")} BuiltinObjectProperty -> Exp {cons("ObjectBuiltinRef"), prefer} %% Desugared into BuiltinAccess(This(), _) "this" -> Exp {cons("This")} diff --git a/DSL/DiversiaScript/syntax/Type.sdf b/DSL/DiversiaScript/syntax/Type.sdf index 1e5a636..0b0e44f 100644 --- a/DSL/DiversiaScript/syntax/Type.sdf +++ b/DSL/DiversiaScript/syntax/Type.sdf @@ -19,6 +19,8 @@ exports "Bool" -> Type {cons("BoolType")} "String" -> Type {cons("StringType")} "Void" -> Type {cons("VoidType")} + "Array" "<" Type ">" -> Type {cons("ArrayType")} + "Map" "<" Type "," Type ">" -> Type {cons("DictType")} BuiltinType -> Type EnumType -> Type ComponentType -> Type {prefer} diff --git a/DSL/DiversiaScript/trans/codegen/lua/generate.str b/DSL/DiversiaScript/trans/codegen/lua/generate.str index 5803e02..bf565c1 100644 --- a/DSL/DiversiaScript/trans/codegen/lua/generate.str +++ b/DSL/DiversiaScript/trans/codegen/lua/generate.str @@ -245,6 +245,8 @@ rules // Expressions to-lua: Vector2Const(exp1, exp2) -> New(Vector2(), [exp1, exp2]) to-lua: Vector3Const(exp1, exp2, exp3) -> New(Vector3(), [exp1, exp2, exp3]) to-lua: Vector4Const(exp1, exp2, exp3, exp4) -> New(Vector4(), [exp1, exp2, exp3, exp4]) + to-lua: ArrayConst(exps) -> Table( Field( exp) \)> exps) + to-lua: DictConst(exps) -> Table( Field( key, val) \)> exps) to-lua: Null() -> LuaNil() to-lua: This(_) -> VarRef("Object") diff --git a/DSL/DiversiaScript/trans/editor/check/error.str b/DSL/DiversiaScript/trans/editor/check/error.str index 5892ddd..50ff29b 100644 --- a/DSL/DiversiaScript/trans/editor/check/error.str +++ b/DSL/DiversiaScript/trans/editor/check/error.str @@ -14,7 +14,9 @@ imports data/type-prop data/type-func data/type-op - data/event + data/event + + util rules // Definitions @@ -216,6 +218,16 @@ rules // Types component => type; exp => type2; not ( (type, type2)) + + editor-error: e@ArrayConst(exps) -> (e, $[Inconsistent types in Array.]) + where + not ( exps) + + editor-error: e@DictConst(exps) -> (e, $[Inconsistent types in Dict.]) + where + exps => (keys, values); + (not ( keys) <+ + not ( values)) rules // References diff --git a/DSL/DiversiaScript/trans/projection/type.str b/DSL/DiversiaScript/trans/projection/type.str index 207fa0d..6840408 100644 --- a/DSL/DiversiaScript/trans/projection/type.str +++ b/DSL/DiversiaScript/trans/projection/type.str @@ -24,7 +24,9 @@ rules // Utility case RealType() : !"Real" case StringType() : !"String" case VoidType() : !"Void" - case NullType() : !"Void*" + case NullType() : !"Void*" + case ArrayType(?type) : !$[Array<[ type]>] + case DictType(?key, ?value) : !$[Dict<[ key], [ value]>] case CustomType(?name) : !$[[name]] case [?type|[]] : type case [?type|?types]; not ( types) : !$[[ type], [ types]] @@ -54,14 +56,18 @@ rules // Utility rules // Type matching (compatibility) // Matches if types are the same or if the second type is a parent type of the first. - match-type = ?(type1, type2); ( type2 <+ (type2, type1)) + match-type = ?(type1, type2); ( type2 <+ (type2, type1)) + + // Matches containers + match-type = ?(ArrayType(type1), ArrayType(type2)); (type1, type2) + match-type = ?(DictType(type1Key, type1Val), DictType(type2Key, type2Val)); (type1Key, type2Key); (type1Val, type2Val) // Matches reals and integers. - match-type = ?(RealType(), IntType()) - match-type = ?(IntType(), RealType()) + match-type: (RealType(), IntType()) -> RealType() + match-type: (IntType(), RealType()) -> IntType() // Matches everything with null. - match-type = ?(_, NullType()) + match-type: (type, NullType()) -> type // Matches lists of types. match-types = ?([], []) @@ -111,6 +117,8 @@ rules // Expressions type-of: FloatConst(_) -> RealType() type-of: StrConst(_) -> StringType() type-of: Null() -> NullType() + type-of: ArrayConst([exp|_]) -> ArrayType( exp) + type-of: DictConst([(key, val)|_]) -> DictType( key, val) type-of: Vector2Const(_, _) -> Vector2() type-of: Vector3Const(_, _, _) -> Vector3() type-of: Vector4Const(_, _, _, _) -> Vector4() @@ -189,6 +197,10 @@ rules // Type projections rules // Type checks + // Checks if given type is of container type. + is-container-type = ?ArrayType(_) + is-container-type = ?DictType(_, _) + // Checks if given type is of standard (POD) type. is-standard-type = ?VoidType() is-standard-type = ?BoolType() @@ -196,6 +208,7 @@ rules // Type checks is-standard-type = ?RealType() is-standard-type = ?StringType() is-standard-type = ?NullType() + is-standard-type = is-container-type // Checks if given type is of object type. is-object-type = ?CustomType(_) diff --git a/DSL/DiversiaScript/trans/util.str b/DSL/DiversiaScript/trans/util.str index fd8b3bd..ed727c1 100644 --- a/DSL/DiversiaScript/trans/util.str +++ b/DSL/DiversiaScript/trans/util.str @@ -3,6 +3,7 @@ module util rules // Utilities take-1: [elem|rest] -> (elem, rest) + take-rev-1: [elem|rest] -> (rest, elem) get-1: [elem|_] -> elem take-2: [elem1|[elem2|rest]] -> (elem1, elem2, rest) crush: cons -> str where cons diff --git a/DSL/Lua/syntax/Lua.pp b/DSL/Lua/syntax/Lua.pp index 0371b51..c8ec9ad 100644 --- a/DSL/Lua/syntax/Lua.pp +++ b/DSL/Lua/syntax/Lua.pp @@ -47,15 +47,15 @@ VarRef -- VAR[_1], TableVarRef -- H hs=0[_1 KW["["] _2 KW["]"]], Access -- H hs=0[_1 KW["."] _2], - Field -- KW["["] _1 KW["]"] KW["="] _2, - Field -- _1 KW["="] _2, - Field -- _1, - Table -- KW["{"] KW["}"], - Table -- KW["{"] _1 KW["}"], - Table -- KW["{"] _1 KW["}"], - Table.1:iter-sep -- _1 KW[","], - Table -- KW["{"] _1 KW["}"], - Table.1:iter-sep -- _1 KW[";"], + Field -- H hs=0[KW["["] _1 KW["]"] KW[" = "] _2], + Field -- H hs=0[_1 KW[" = "] _2], + Field -- H hs=0[_1], + Table -- H hs=0[KW["{"] KW["}"]], + Table -- H hs=0[KW["{"] _1 KW["}"]], + Table -- H hs=0[KW["{"] _1 KW["}"]], + Table.1:iter-sep -- H hs=0[_1 KW[", "]], + Table -- H hs=0[KW["{"] _1 KW["}"]], + Table.1:iter-sep -- H hs=0[_1 KW["; "]], LuaNil -- KW["nil"], False -- KW["false"], True -- KW["true"],