diff --git a/ast/nodes.expressions.cs b/ast/nodes.expressions.cs index 088b9ad..703e357 100644 --- a/ast/nodes.expressions.cs +++ b/ast/nodes.expressions.cs @@ -102,8 +102,8 @@ namespace arookas public sunExpression TrueBody { get { return this[1] as sunExpression; } } public sunExpression FalseBody { get { return this[2] as sunExpression; } } - public sunTernaryOperator(sunSourceLocation node) - : base(node) + public sunTernaryOperator(sunSourceLocation location) + : base(location) { } @@ -119,4 +119,90 @@ namespace arookas context.Text.ClosePoint(trueEpilogue); } } + + // increment/decrement + class sunPostfixAugment : sunOperand + { + public sunIdentifier Variable { get { return this[0] as sunIdentifier; } } + public sunAugment Operator { get { return this[1] as sunAugment; } } + + public sunPostfixAugment(sunSourceLocation location) + : base(location) + { + + } + + public override void Compile(sunContext context) + { + var variableInfo = context.ResolveVariable(Variable); + if (Parent is sunOperand) + { + context.Text.PushVariable(variableInfo); + } + Operator.Compile(context, variableInfo); + context.Text.StoreVariable(variableInfo); + } + } + + class sunPrefixAugment : sunOperand + { + public sunAugment Operator { get { return this[0] as sunAugment; } } + public sunIdentifier Variable { get { return this[1] as sunIdentifier; } } + + public sunPrefixAugment(sunSourceLocation location) + : base(location) + { + + } + + public override void Compile(sunContext context) + { + var variableInfo = context.ResolveVariable(Variable); + Operator.Compile(context, variableInfo); + context.Text.StoreVariable(variableInfo); + if (Parent is sunOperand) + { + context.Text.PushVariable(variableInfo); + } + } + } + + abstract class sunAugment : sunNode + { + protected sunAugment(sunSourceLocation location) + : base(location) + { + + } + + public abstract void Compile(sunContext context, sunVariableInfo variable); + } + + class sunIncrement : sunAugment + { + public sunIncrement(sunSourceLocation location) + : base(location) + { + + } + + public override void Compile(sunContext context, sunVariableInfo variable) + { + context.Text.IncVariable(variable); + } + } + + class sunDecrement : sunAugment + { + public sunDecrement(sunSourceLocation location) + : base(location) + { + + } + + public override void Compile(sunContext context, sunVariableInfo variable) + { + context.Text.DecVariable(variable); + } + } } diff --git a/language.md b/language.md index da95be7..675b4a6 100644 --- a/language.md +++ b/language.md @@ -132,8 +132,8 @@ Loops may be named. To name a loop, simply prefix the loop with a label. `break` ``` outer_loop: -for (var a; a < 4; a += 1) { - for (var b; b < 4; b += 1) { +for (var a; a < 4; ++a) { + for (var b; b < 4; ++b) { if (b == 2) break outer_loop; } } diff --git a/parser.cs b/parser.cs index ca6c1b8..839d40f 100644 --- a/parser.cs +++ b/parser.cs @@ -61,9 +61,11 @@ namespace arookas case __sunConstants.STATEMENT: case __sunConstants.COMPOUND_STATEMENT: case __sunConstants.COMPOUND_STATEMENT_ITEM: + case __sunConstants.VARIABLE_AUGMENT: case __sunConstants.ASSIGNMENT_OPERATOR: case __sunConstants.BINARY_OPERATOR: case __sunConstants.UNARY_OPERATOR: + case __sunConstants.AUGMENT_OPERATOR: case __sunConstants.TERM: case __sunConstants.PARAMETER: case __sunConstants.ARGUMENT_LIST: @@ -162,10 +164,14 @@ namespace arookas case __sunConstants.ASSIGN_BIT_LSH: return new sunAssignBitLsh(location); case __sunConstants.ASSIGN_BIT_RSH: return new sunAssignBitRsh(location); + case __sunConstants.INCREMENT: return new sunIncrement(location); + case __sunConstants.DECREMENT: return new sunDecrement(location); + case __sunConstants.ASSIGNMENT_OPERATOR: return new sunNode(location); case __sunConstants.TERNARY_OPERATOR: return new sunTernaryOperator(location); case __sunConstants.BINARY_OPERATOR: return new sunNode(location); case __sunConstants.UNARY_OPERATOR: return new sunNode(location); + case __sunConstants.AUGMENT_OPERATOR: return new sunNode(location); } // expressions @@ -180,6 +186,9 @@ namespace arookas case __sunConstants.INT_CAST: return new sunIntCast(location); case __sunConstants.FLOAT_CAST: return new sunFloatCast(location); case __sunConstants.TYPEOF_CAST: return new sunTypeofCast(location); + + case __sunConstants.PREFIX_AUGMENT: return new sunPrefixAugment(location); + case __sunConstants.POSTFIX_AUGMENT: return new sunPostfixAugment(location); } // builtins @@ -207,6 +216,7 @@ namespace arookas case __sunConstants.VARIABLE_DECLARATION: return new sunVariableDeclaration(location); case __sunConstants.VARIABLE_DEFINITION: return new sunVariableDefinition(location); case __sunConstants.VARIABLE_ASSIGNMENT: return new sunVariableAssignment(location); + case __sunConstants.VARIABLE_AUGMENT: return new sunNode(location); } // constants diff --git a/sunscript.grammar b/sunscript.grammar index d90c0ff..0a5a18e 100644 --- a/sunscript.grammar +++ b/sunscript.grammar @@ -100,6 +100,9 @@ ASSIGN_BIT_OR = "|=" ASSIGN_BIT_LSH = "<<=" ASSIGN_BIT_RSH = ">>=" +INCREMENT = "++" +DECREMENT = "--" + // literals IDENTIFIER = <<[_A-Za-z][_A-Za-z0-9]*>> DEC_NUMBER = <<-?[0-9]+\.[0-9]+>> @@ -138,6 +141,7 @@ compound_statement_item = variable_definition | variable_declaration | variable_assignment | + variable_augment | print_statement | function_call; statement_block = L_BRACE {statement} R_BRACE; @@ -161,6 +165,7 @@ binary_operator = EQ | NEQ | LT | LTEQ | GT | GTEQ | // comparison LOG_AND | LOG_OR; // logical unary_operator = LOG_NOT | SUB; +augment_operator = INCREMENT | DECREMENT; // expressions expression = operand {binary_operator operand}; @@ -171,6 +176,7 @@ term = typeof_cast | function_call | variable_reference | + variable_augment | STRING | DEC_NUMBER | HEX_NUMBER | @@ -194,6 +200,10 @@ variable_reference = IDENTIFIER; // used in expressions variable_declaration = VAR IDENTIFIER; variable_definition = VAR IDENTIFIER assignment_operator expression; variable_assignment = IDENTIFIER assignment_operator expression; +variable_augment = postfix_augment | prefix_augment; + +postfix_augment = IDENTIFIER augment_operator; +prefix_augment = augment_operator IDENTIFIER; // functions function_definition = FUNCTION IDENTIFIER parameter_list statement_block;