From 2ce9882cad0f4fb962b36af97266375db7c6bc93 Mon Sep 17 00:00:00 2001 From: arookas Date: Mon, 28 Dec 2015 20:19:12 -0500 Subject: [PATCH] Moved name labels to the statement production. --- ssc/ast/nodes.flow.cs | 35 ++++++++++++++++++++--------------- ssc/ast/nodes.statements.cs | 4 ++++ ssc/sunscript.grammar | 12 ++++++------ 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/ssc/ast/nodes.flow.cs b/ssc/ast/nodes.flow.cs index e4081bc..0f0e1ee 100644 --- a/ssc/ast/nodes.flow.cs +++ b/ssc/ast/nodes.flow.cs @@ -28,23 +28,29 @@ namespace arookas { } abstract class sunLoop : sunNode { - public bool IsNamed { get { return NameLabel != null; } } - public sunNameLabel NameLabel { get { return this[0] as sunNameLabel; } } - protected sunLoop(sunSourceLocation location) : base(location) { } + + public void PushLoop(sunContext context) { + var name = context.PopNameLabel(); + if (name == null) { + context.Loops.Push(); + } + else { + context.Loops.Push(name.Label.Value); + } + } } - class sunWhile : sunLoop - { - public sunExpression Condition { get { return this[Count - 2] as sunExpression; } } - public sunNode Body { get { return this[Count - 1]; } } + class sunWhile : sunLoop { + public sunExpression Condition { get { return this[0] as sunExpression; } } + public sunNode Body { get { return this[1]; } } public sunWhile(sunSourceLocation location) : base(location) { } public override void Compile(sunContext context) { - context.Loops.Push(IsNamed ? NameLabel.Label.Value : null); + PushLoop(context); var bodyPrologue = context.Text.OpenPoint(); var continuePoint = context.Text.OpenPoint(); Condition.Compile(context); @@ -57,16 +63,15 @@ namespace arookas { } } - class sunDo : sunLoop - { - public sunNode Body { get { return this[Count - 2]; } } - public sunExpression Condition { get { return this[Count - 1] as sunExpression; } } + class sunDo : sunLoop { + public sunNode Body { get { return this[0]; } } + public sunExpression Condition { get { return this[1] as sunExpression; } } public sunDo(sunSourceLocation location) : base(location) { } public override void Compile(sunContext context) { - context.Loops.Push(IsNamed ? NameLabel.Label.Value : null); + PushLoop(context); var bodyPrologue = context.Text.OpenPoint(); Body.Compile(context); var continuePoint = context.Text.OpenPoint(); @@ -89,8 +94,8 @@ namespace arookas { : base(location) { } public override void Compile(sunContext context) { - context.Scopes.Push(context.Scopes.Top.Type); - context.Loops.Push(IsNamed ? NameLabel.Label.Value : null); + context.Scopes.Push(); + PushLoop(context); TryCompile(Declaration, context); var bodyPrologue = context.Text.OpenPoint(); TryCompile(Condition, context); diff --git a/ssc/ast/nodes.statements.cs b/ssc/ast/nodes.statements.cs index 64059c1..4e8f871 100644 --- a/ssc/ast/nodes.statements.cs +++ b/ssc/ast/nodes.statements.cs @@ -35,5 +35,9 @@ public sunNameLabel(sunSourceLocation location) : base(location) { } + + public override void Compile(sunContext context) { + context.PushNameLabel(this); + } } } diff --git a/ssc/sunscript.grammar b/ssc/sunscript.grammar index 19805de..896d8de 100644 --- a/ssc/sunscript.grammar +++ b/ssc/sunscript.grammar @@ -123,9 +123,9 @@ root_statement = statement = compound_statement SEMICOLON | if_statement | - while_statement | - do_statement SEMICOLON | - for_statement | + [name_label] while_statement | + [name_label] do_statement SEMICOLON | + [name_label] for_statement | return_statement SEMICOLON | break_statement SEMICOLON | continue_statement SEMICOLON | @@ -219,9 +219,9 @@ builtin_declaration = BUILTIN IDENTIFIER parameter_list; // flow control if_statement = IF expression statement [ELSE statement]; -while_statement = [name_label] WHILE expression statement; -do_statement = [name_label] DO statement WHILE expression; -for_statement = [name_label] FOR L_PAREN [for_declaration] SEMICOLON [for_condition] SEMICOLON [for_iteration] R_PAREN statement; +while_statement = WHILE expression statement; +do_statement = DO statement WHILE expression; +for_statement = FOR L_PAREN [for_declaration] SEMICOLON [for_condition] SEMICOLON [for_iteration] R_PAREN statement; for_declaration = compound_statement; for_condition = expression; for_iteration = compound_statement;