ssc/ssc/ast/nodes.variables.cs

85 lines
2.9 KiB
C#
Raw Normal View History

namespace arookas {
class sunStorableReference : sunNode, sunTerm {
public sunIdentifier Storable { get { return this[0] as sunIdentifier; } }
2015-12-06 23:15:02 -05:00
public sunStorableReference(sunSourceLocation location)
: base(location) { }
2015-12-06 23:15:02 -05:00
public override void Compile(sunCompiler compiler) {
compiler.Context.MustResolveStorable(Storable).Compile(compiler);
2015-12-06 23:15:02 -05:00
}
sunExpressionFlags sunTerm.GetExpressionFlags(sunContext context) {
var symbol = context.MustResolveStorable(Storable);
if (symbol is sunVariableSymbol) {
return sunExpressionFlags.Variables | sunExpressionFlags.Dynamic;
}
else if (symbol is sunConstantSymbol) {
return sunExpressionFlags.Constants;
}
return sunExpressionFlags.None;
}
2015-12-06 23:15:02 -05:00
}
class sunVariableDeclaration : sunNode {
public sunIdentifier Variable { get { return this[0] as sunIdentifier; } }
2015-12-06 23:15:02 -05:00
public sunVariableDeclaration(sunSourceLocation location)
: base(location) { }
2015-12-06 23:15:02 -05:00
public override void Compile(sunCompiler compiler) {
compiler.Context.DeclareVariable(Variable);
2015-12-06 23:15:02 -05:00
}
}
class sunVariableDefinition : sunNode {
public sunIdentifier Variable { get { return this[0] as sunIdentifier; } }
public sunAssign Operator { get { return this[1] as sunAssign; } }
public sunExpression Expression { get { return this[2] as sunExpression; } }
2015-12-06 23:15:02 -05:00
public sunVariableDefinition(sunSourceLocation location)
: base(location) { }
2015-12-06 23:15:02 -05:00
public override void Compile(sunCompiler compiler) {
var symbol = compiler.Context.DeclareVariable(Variable);
Operator.Compile(compiler, symbol, Expression);
2015-12-06 23:15:02 -05:00
}
}
class sunStorableAssignment : sunNode {
public sunIdentifier Storable { get { return this[0] as sunIdentifier; } }
2015-12-06 23:15:02 -05:00
public sunAssign Operator { get { return this[1] as sunAssign; } }
public sunExpression Expression { get { return this[2] as sunExpression; } }
2015-12-28 02:50:06 -05:00
public sunStorableAssignment(sunSourceLocation location)
: base(location) { }
2015-12-06 23:15:02 -05:00
public override void Compile(sunCompiler compiler) {
var symbol = compiler.Context.MustResolveStorable(Storable);
if (symbol is sunConstantSymbol) {
throw new sunAssignConstantException(Storable);
}
Operator.Compile(compiler, symbol, Expression);
2015-12-06 23:15:02 -05:00
}
}
class sunConstantDefinition : sunNode {
2015-12-06 23:15:02 -05:00
public sunIdentifier Constant { get { return this[0] as sunIdentifier; } }
public sunExpression Expression { get { return this[2] as sunExpression; } }
2015-12-28 02:49:34 -05:00
public sunConstantDefinition(sunSourceLocation location)
: base(location) { }
2015-12-06 23:15:02 -05:00
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(Constant, Expression);
2015-12-06 23:15:02 -05:00
}
}
}