From a0dbcb5d5bcdbedfccba25137952208236ee0f86 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Sat, 28 Sep 2013 21:08:30 -0500 Subject: [PATCH] Add TRUE and FALSE terminals to the zcc grammar - I can't believe I completely forgot to let the parser handle true and false literals. - Consolidate all the %include blocks in zcc-parse.lemon into a single one, because Lemon all of a sudden decided it didn't like me having more than one in the grammar file. - Added a PBool type to represent boolean values with. --- src/dobjtype.cpp | 7 +++++ src/dobjtype.h | 8 ++++++ src/zscript/zcc-parse.lemon | 53 ++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/dobjtype.cpp b/src/dobjtype.cpp index 210d2db2b..ecc0b5048 100644 --- a/src/dobjtype.cpp +++ b/src/dobjtype.cpp @@ -66,6 +66,7 @@ PVoidType *TypeVoid; PInt *TypeSInt8, *TypeUInt8; PInt *TypeSInt16, *TypeUInt16; PInt *TypeSInt32, *TypeUInt32; +PBool *TypeBool; PFloat *TypeFloat32, *TypeFloat64; PString *TypeString; PName *TypeName; @@ -307,6 +308,7 @@ void PType::StaticInit() RUNTIME_CLASS(PErrorType)->TypeTableType = RUNTIME_CLASS(PErrorType); RUNTIME_CLASS(PVoidType)->TypeTableType = RUNTIME_CLASS(PVoidType); RUNTIME_CLASS(PInt)->TypeTableType = RUNTIME_CLASS(PInt); + RUNTIME_CLASS(PBool)->TypeTableType = RUNTIME_CLASS(PBool); RUNTIME_CLASS(PFloat)->TypeTableType = RUNTIME_CLASS(PFloat); RUNTIME_CLASS(PString)->TypeTableType = RUNTIME_CLASS(PString); RUNTIME_CLASS(PName)->TypeTableType = RUNTIME_CLASS(PName); @@ -334,6 +336,7 @@ void PType::StaticInit() TypeTable.AddType(TypeUInt16 = new PInt(2, true)); TypeTable.AddType(TypeSInt32 = new PInt(4, false)); TypeTable.AddType(TypeUInt32 = new PInt(4, true)); + TypeTable.AddType(TypeBool = new PBool); TypeTable.AddType(TypeFloat32 = new PFloat(4)); TypeTable.AddType(TypeFloat64 = new PFloat(8)); TypeTable.AddType(TypeString = new PString); @@ -561,6 +564,10 @@ int PInt::GetRegType() const return REGT_INT; } +/* PBool ******************************************************************/ + +IMPLEMENT_CLASS(PBool) + /* PFloat *****************************************************************/ IMPLEMENT_CLASS(PFloat) diff --git a/src/dobjtype.h b/src/dobjtype.h index 1e9cebf02..2861b1da2 100644 --- a/src/dobjtype.h +++ b/src/dobjtype.h @@ -230,6 +230,13 @@ protected: PInt(); }; +class PBool : public PInt +{ + DECLARE_CLASS(PBool, PInt); +public: + PBool() : PInt(sizeof(bool), true) {} +}; + class PFloat : public PBasicType { DECLARE_CLASS(PFloat, PBasicType); @@ -643,6 +650,7 @@ extern PVoidType *TypeVoid; extern PInt *TypeSInt8, *TypeUInt8; extern PInt *TypeSInt16, *TypeUInt16; extern PInt *TypeSInt32, *TypeUInt32; +extern PBool *TypeBool; extern PFloat *TypeFloat32, *TypeFloat64; extern PString *TypeString; extern PName *TypeName; diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index a8e069511..44bf56fb3 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -43,6 +43,25 @@ static void SetNodeLine(ZCC_TreeNode *name, int line) name->Type = type; \ name->IntVal = val + struct ClassFlagsBlock { + VM_UWORD Flags; + ZCC_Identifier *Replaces; + }; + + struct StateOpts { + ZCC_Expression *Offset; + bool Bright; + }; + + struct VarOrFun + { + ZCC_VarName *VarNames; + ZCC_FuncParamDecl *FuncParams; + ZCC_CompoundStmt *FuncBody; + ENamedName FuncName; + int FuncFlags; + int SourceLoc; + }; } %token_prefix ZCC_ @@ -164,12 +183,6 @@ class_ancestry(X) ::= . { X = NULL; } class_ancestry(X) ::= COLON dottable_id(A). { X = A; } %type class_flags{ClassFlagsBlock} -%include { - struct ClassFlagsBlock { - VM_UWORD Flags; - ZCC_Identifier *Replaces; - }; -} class_flags(X) ::= . { X.Flags = 0; X.Replaces = NULL; } class_flags(X) ::= class_flags(A) ABSTRACT. { X.Flags = A.Flags | 0/*FIXME*/; X.Replaces = A.Replaces; } class_flags(X) ::= class_flags(A) NATIVE. { X.Flags = A.Flags | 0/*FIXME*/; X.Replaces = A.Replaces; } @@ -369,12 +382,6 @@ enumerator(X) ::= IDENTIFIER(A) EQ expr(B). /* Expression must be constant. */ %type state_call {ZCC_ExprFuncCall *} %type state_call_params {ZCC_FuncParm *} -%include { - struct StateOpts { - ZCC_Expression *Offset; - bool Bright; - }; -} %type state_opts {StateOpts} states_def(X) ::= STATES(T) scanner_mode LBRACE states_body(A) RBRACE. @@ -618,18 +625,6 @@ array_size(X) ::= array_size(A) array_size_expr(B). X = A; } -%include -{ - struct VarOrFun - { - ZCC_VarName *VarNames; - ZCC_FuncParamDecl *FuncParams; - ZCC_CompoundStmt *FuncBody; - ENamedName FuncName; - int FuncFlags; - int SourceLoc; - }; -} %type variables_or_function {VarOrFun} /* Multiple type names are only valid for functions. */ @@ -1232,6 +1227,16 @@ constant(X) ::= NAMECONST(A). floatconst->IntVal = A.Int; X = floatconst; } +constant(X) ::= FALSE(A). +{ + NEW_INTCONST_NODE(boolconst, TypeBool, false, A); + X = boolconst; +} +constant(X) ::= TRUE(A). +{ + NEW_INTCONST_NODE(boolconst, TypeBool, true, A); + X = boolconst; +} /************ Statements ************/