Skip to content

Commit

Permalink
Added array/dictionary types, literals and error checking.
Browse files Browse the repository at this point in the history
Added lua code generation for array/dictionary literals.
Improved lua pretty printing for tables.
  • Loading branch information
Gohla committed Jul 22, 2011
1 parent d86999e commit 07863ad
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
2 changes: 2 additions & 0 deletions DSL/DiversiaScript/syntax/Expression.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down
2 changes: 2 additions & 0 deletions DSL/DiversiaScript/syntax/Type.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions DSL/DiversiaScript/trans/codegen/lua/generate.str
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ rules // Expressions
to-lua: Vector2Const(exp1, exp2) -> <to-lua> New(Vector2(), [exp1, exp2])
to-lua: Vector3Const(exp1, exp2, exp3) -> <to-lua> New(Vector3(), [exp1, exp2, exp3])
to-lua: Vector4Const(exp1, exp2, exp3, exp4) -> <to-lua> New(Vector4(), [exp1, exp2, exp3, exp4])
to-lua: ArrayConst(exps) -> Table(<map(\ exp -> Field(<to-lua> exp) \)> exps)
to-lua: DictConst(exps) -> Table(<map(\ (key, val) -> Field(<to-lua> key, <to-lua> val) \)> exps)
to-lua: Null() -> LuaNil()
to-lua: This(_) -> VarRef("Object")

Expand Down
14 changes: 13 additions & 1 deletion DSL/DiversiaScript/trans/editor/check/error.str
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ imports
data/type-prop
data/type-func
data/type-op
data/event
data/event

util

rules // Definitions

Expand Down Expand Up @@ -216,6 +218,16 @@ rules // Types
<get-property-type(|prop)> component => type;
<type-of> exp => type2;
not (<match-type> (type, type2))

editor-error: e@ArrayConst(exps) -> (e, $[Inconsistent types in Array.])
where
not (<foldl(match-type)> <take-rev-1> <map(type-of)> exps)

editor-error: e@DictConst(exps) -> (e, $[Inconsistent types in Dict.])
where
<unzip> exps => (keys, values);
(not (<foldl(match-type)> <take-rev-1> <map(type-of)> keys) <+
not (<foldl(match-type)> <take-rev-1> <map(type-of)> values))

rules // References

Expand Down
23 changes: 18 additions & 5 deletions DSL/DiversiaScript/trans/projection/type.str
Original file line number Diff line number Diff line change
Expand Up @@ -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<[<printable> type]>]
case DictType(?key, ?value) : !$[Dict<[<printable> key], [<printable> value]>]
case CustomType(?name) : !$[[name]]
case [?type|[]] : <printable> type
case [?type|?types]; not (<?[]> types) : !$[[<printable> type], [<printable> types]]
Expand Down Expand Up @@ -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); (<?type1> type2 <+ <get-type-parent> (type2, type1))
match-type = ?(type1, type2); (<?type1> type2 <+ <get-type-parent> (type2, type1))

// Matches containers
match-type = ?(ArrayType(type1), ArrayType(type2)); <match-type> (type1, type2)
match-type = ?(DictType(type1Key, type1Val), DictType(type2Key, type2Val)); <match-type> (type1Key, type2Key); <match-type> (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 = ?([], [])
Expand Down Expand Up @@ -111,6 +117,8 @@ rules // Expressions
type-of: FloatConst(_) -> RealType()
type-of: StrConst(_) -> StringType()
type-of: Null() -> NullType()
type-of: ArrayConst([exp|_]) -> ArrayType(<type-of> exp)
type-of: DictConst([(key, val)|_]) -> DictType(<type-of> key, <type-of> val)
type-of: Vector2Const(_, _) -> Vector2()
type-of: Vector3Const(_, _, _) -> Vector3()
type-of: Vector4Const(_, _, _, _) -> Vector4()
Expand Down Expand Up @@ -189,13 +197,18 @@ 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()
is-standard-type = ?IntType()
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(_)
Expand Down
1 change: 1 addition & 0 deletions DSL/DiversiaScript/trans/util.str
Original file line number Diff line number Diff line change
Expand Up @@ -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 <?str#(_)> cons
Expand Down
18 changes: 9 additions & 9 deletions DSL/Lua/syntax/Lua.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 07863ad

Please sign in to comment.