# HG changeset patch # User Anders Johnsen # Date 1218472905 -7200 # Node ID 4f94b5adbc8a0fcd2e9a3bd8df3ee54d5128f8d4 # Parent 4c121c2aa8442d4774b3d52539370355b7cae1b7 Added ast/Expr.d to contain all expressions. diff -r 4c121c2aa844 -r 4f94b5adbc8a ast/Decl.d --- a/ast/Decl.d Sun Aug 10 17:09:41 2008 +0200 +++ b/ast/Decl.d Mon Aug 11 18:41:45 2008 +0200 @@ -1,4 +1,7 @@ module ast.Decl; + +import ast.Expr; + /** The base class for all Declarations. @@ -8,7 +11,127 @@ $(LI members, such as variables and functions.) $(LI types, such as classes, structs.)) - **/ + */ class Decl { + + /// Method for doing cast(FunctionDecl) + FunctionDecl asFunctionDecl() { return null;} + + /// Returns true if it is a FunctionDecl + bool isFunctionDecl() { return false;} + + /// Method for doing cast(VarDecl) + VarDecl asVarDecl() { return null;} + + /// Returns true if it is a VarDecl + bool isVarDecl() { return false;} + + /// Method for doing cast(StructDecl) + StructDecl asStructDecl() { return null;} + + /// Returns true if it is a StructDecl + bool isStructDecl() { return false;} + + /// Method for doing cast(ClassDecl) + ClassDecl asClassDecl() { return null;} + + /// Returns true if it is a ClassDecl + bool isClassDecl() { return false;} + + /// Method for doing cast(InterfaceDecl) + InterfaceDecl asInterfaceDecl() { return null;} + + /// Returns true if it is a InterfaceDecl + bool isInterfaceDecl() { return false;} + + /// Method for doing cast(TypedefDecl) + TypedefDecl asTypedefDecl() { return null;} + + /// Returns true if it is a TypedefDecl + bool isTypedefDecl() { return false;} } + +/** + The FunctionDecl contains a set of VarDecls, being the parameters of the + method. It also contains a potentiel list of statements. + */ +class FunctionDecl : Decl +{ + /// Returns the parameters of the method. + VarDecl[] getParams() { return params;} + /// Return the parameter on a given index. + VarDecl getParam(int index) { return params[index];} + /// Returns the number of parameters. + int getNumberOfParamst() { return params.length;} + /** + Returns the number of required arguments, that should be given to + call this method. + */ + int getMinimumNumberOfParams() + { + assert(0, "Unimplemented"); + return 0; + } + + override FunctionDecl asFunctionDecl() { return this;} + override bool isFunctionDecl() { return true;} + +private: + VarDecl[] params; +} + +/** + The VarDecl contains as a minimum a Type. As an addition, i will also + in most cases contain an Identifier that it'll be reconized by and in + some cases it will also contain an expression that'll be it initializer. + + Some cases to check: + + $(UL + $(LI + If the VarDecl is a param in a FunctionDecl the initializer, if + present, should be a constant expression(Enum, Number, String + or the like). + ) + ) + */ +class VarDecl : Decl +{ + /// Return true if the VarDecl has an identifier/name. + bool hasIdentifier() { return !(identifier is null);} + /// Return true if the VarDecl has en initializer. + bool hasInitializer() { return !(initializer is null);} + + override VarDecl asVarDecl() { return this;} + override bool isVarDecl() { return true;} + +private: + Identifier identifier; + Expr initializer; +} + +class StructDecl : Decl +{ + override StructDecl asStructDecl() { return this;} + override bool isStructDecl() { return true;} +} + +class ClassDecl : Decl +{ + override ClassDecl asClassDecl() { return this;} + override bool isClassDecl() { return true;} +} + +class InterfaceDecl : Decl +{ + override InterfaceDecl asInterfaceDecl() { return this;} + override bool isInterfaceDecl() { return true;} +} + +class TypedefDecl : Decl +{ + override TypedefDecl asTypedefDecl() { return this;} + override bool isTypedefDecl() { return true;} +} + diff -r 4c121c2aa844 -r 4f94b5adbc8a ast/Expr.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ast/Expr.d Mon Aug 11 18:41:45 2008 +0200 @@ -0,0 +1,72 @@ +module ast.Expr; + +/** + The base class for all Expressions. + */ +class Expr +{ + /// Returns true if the Expr can be compile-time evaluated. + bool isConstantExpr() { return false; } + + NumberLiteral asNumberLiteral() { return null; } + bool isNumberLiteral() { return false; } + + StringLiteral asStringLiteral() { return null; } + bool isStringLiteral() { return false; } + + ArrayLiteral asArrayLiteral() { return null; } + bool isArrayLiteral() { return false; } +} + +/** + NumberLiteral + */ +class NumberLiteral : Expr +{ + override bool isConstantExpr() { return true; } + + override NumberLiteral asNumberLiteral() { return this; } + override bool isNumberLiteral() { return true; } +} + +/** + StringLiteral + */ +class StringLiteral : Expr +{ + override bool isConstantExpr() { return true; } + + override StringLiteral asStringLiteral() { return this; } + override bool isStringLiteral() { return true; } +} + + +/** + StringLiteral + */ +class ArrayLiteral : Expr +{ + override bool isConstantExpr() + { + foreach (arg; arguments) + if (!arg.isConstantExpr()) + return false; + return true; + } + + override ArrayLiteral asArrayLiteral() { return this; } + override bool isArrayLiteral() { return true; } + +private: + Expr[] arguments; +} + + + +/** + Identifier + */ +class Identifier : Expr +{ + +} diff -r 4c121c2aa844 -r 4f94b5adbc8a doc/candydoc/modules.ddoc --- a/doc/candydoc/modules.ddoc Sun Aug 10 17:09:41 2008 +0200 +++ b/doc/candydoc/modules.ddoc Mon Aug 11 18:41:45 2008 +0200 @@ -1,2 +1,3 @@ MODULES = $(MODULE_FULL ast.Decl) + $(MODULE_FULL ast.Expr)