From a676f9e8e5db79d1f2465cfa581177b21c07b261 Mon Sep 17 00:00:00 2001 From: arookas Date: Mon, 22 Feb 2016 20:53:40 -0500 Subject: [PATCH] Merged sunConstDefinition and sunVariableDefinition Now sunVariableDefinition declares either a constant symbol or a variable symbol, depending on the appearance of the 'const' modifier. --- ssc/ast/nodes.variables.cs | 44 ++++++++++++++++---------------------- ssc/context.cs | 2 +- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/ssc/ast/nodes.variables.cs b/ssc/ast/nodes.variables.cs index 4d3cbd9..3c0596d 100644 --- a/ssc/ast/nodes.variables.cs +++ b/ssc/ast/nodes.variables.cs @@ -53,9 +53,24 @@ : base(location) { } public override void Compile(sunCompiler compiler) { - var symbol = compiler.Context.DeclareVariable(this); - symbol.Modifiers = Modifiers; - Operator.Compile(compiler, symbol, Expression); + // create the right type of symbol based on the const modifier + var isConst = (Modifiers & sunSymbolModifiers.Constant) != 0; + if (isConst) { + // analyze the expression. this does two things: + // 1) prevents recursion (i.e. the const referencing itself) + // 2) asserts actual constness + var flags = Expression.Analyze(compiler.Context); + if (flags.HasFlag(sunExpressionFlags.Dynamic)) { + throw new sunConstantExpressionException(Expression); + } + var symbol = compiler.Context.DeclareConstant(this); + symbol.Modifiers = Modifiers; + } + else { + var symbol = compiler.Context.DeclareVariable(this); + symbol.Modifiers = Modifiers; + Operator.Compile(compiler, symbol, Expression); + } } } @@ -75,27 +90,4 @@ Operator.Compile(compiler, symbol, Expression); } } - - class sunConstantDefinition : sunNode { - public sunIdentifier Name { get { return this[1] as sunIdentifier; } } - public sunExpression Expression { get { return this[3] as sunExpression; } } - - public sunSymbolModifiers Modifiers { - get { return sunSymbol.GetModifiers(this[0]) | sunSymbolModifiers.Constant; } - } - - public sunConstantDefinition(sunSourceLocation location) - : base(location) { } - - public override void Compile(sunCompiler compiler) { - // analyze the expression. this does two things: - // 1) prevents recursion (i.e. the const referencing itself) - // 2) asserts actual constness - var flags = Expression.Analyze(compiler.Context); - if (flags.HasFlag(sunExpressionFlags.Dynamic)) { - throw new sunConstantExpressionException(Expression); - } - compiler.Context.DeclareConstant(this); - } - } } diff --git a/ssc/context.cs b/ssc/context.cs index 63487ba..0eef991 100644 --- a/ssc/context.cs +++ b/ssc/context.cs @@ -111,7 +111,7 @@ namespace arookas { } return symbol; } - public sunConstantSymbol DeclareConstant(sunConstantDefinition node) { + public sunConstantSymbol DeclareConstant(sunVariableDefinition node) { return DeclareConstant(node.Name, node.Expression, node.Modifiers); } sunConstantSymbol DeclareConstant(sunIdentifier node, sunExpression expression, sunSymbolModifiers modifiers) {