From e2236cf70364658bf6538335f12ab76b95e7baa8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 8 Dec 2016 19:38:50 +0200 Subject: [PATCH] move soml ast here, now called type as the language is going --- lib/register/machine.rb | 7 -- lib/typed/ast/assignment.rb | 13 +++ lib/typed/ast/basic_values.rb | 39 +++++++ lib/typed/ast/call_site.rb | 5 + lib/typed/ast/class_statement.rb | 9 ++ lib/typed/ast/code.rb | 27 +++++ lib/typed/ast/field_access.rb | 5 + lib/typed/ast/function_statement.rb | 5 + lib/typed/ast/if_statement.rb | 5 + lib/typed/ast/operator_expression.rb | 5 + lib/typed/ast/return_statement.rb | 5 + lib/typed/ast/statements.rb | 5 + lib/typed/ast/to_code.rb | 163 +++++++++++++++++++++++++++ lib/typed/ast/while_statement.rb | 5 + 14 files changed, 291 insertions(+), 7 deletions(-) create mode 100644 lib/typed/ast/assignment.rb create mode 100644 lib/typed/ast/basic_values.rb create mode 100644 lib/typed/ast/call_site.rb create mode 100644 lib/typed/ast/class_statement.rb create mode 100644 lib/typed/ast/code.rb create mode 100644 lib/typed/ast/field_access.rb create mode 100644 lib/typed/ast/function_statement.rb create mode 100644 lib/typed/ast/if_statement.rb create mode 100644 lib/typed/ast/operator_expression.rb create mode 100644 lib/typed/ast/return_statement.rb create mode 100644 lib/typed/ast/statements.rb create mode 100644 lib/typed/ast/to_code.rb create mode 100644 lib/typed/ast/while_statement.rb diff --git a/lib/register/machine.rb b/lib/register/machine.rb index e3fa833b..6341f3bf 100644 --- a/lib/register/machine.rb +++ b/lib/register/machine.rb @@ -70,13 +70,6 @@ module Register self end - def parse_and_compile bytes - syntax = Parser::Salama.new.parse_with_debug(bytes, reporter: Parslet::ErrorReporter::Deepest.new) - parts = Parser::Transform.new.apply(syntax) - #puts parts.inspect - Typed.compile( parts ) - end - end # Module function to retrieve singleton diff --git a/lib/typed/ast/assignment.rb b/lib/typed/ast/assignment.rb new file mode 100644 index 00000000..77c7cfe5 --- /dev/null +++ b/lib/typed/ast/assignment.rb @@ -0,0 +1,13 @@ +module Typed + class Assignment < Statement + attr_accessor :name , :value + def initialize(n = nil , v = nil ) + @name , @value = n , v + end + end + + class FieldDef < Statement + attr_accessor :name , :type , :value + end + +end diff --git a/lib/typed/ast/basic_values.rb b/lib/typed/ast/basic_values.rb new file mode 100644 index 00000000..0e33a8ca --- /dev/null +++ b/lib/typed/ast/basic_values.rb @@ -0,0 +1,39 @@ +module Typed + class IntegerExpression < Expression + attr_accessor :value + def initialize(value) + @value = value + end + end + class FloatExpression < Expression + attr_accessor :value + def initialize(value) + @value = value + end + end + class TrueExpression < Expression + end + class FalseExpression < Expression + end + class NilExpression < Expression + end + class StringExpression < Expression + attr_accessor :value + def initialize(value) + @value = value + end + end + class NameExpression < Expression + attr_accessor :value + alias :name :value + def initialize(value) + @value = value + end + end + class ClassExpression < Expression + attr_accessor :value + def initialize(value) + @value = value + end + end +end diff --git a/lib/typed/ast/call_site.rb b/lib/typed/ast/call_site.rb new file mode 100644 index 00000000..71086bda --- /dev/null +++ b/lib/typed/ast/call_site.rb @@ -0,0 +1,5 @@ +module Typed + class CallSite < Expression + attr_accessor :name , :receiver , :arguments + end +end diff --git a/lib/typed/ast/class_statement.rb b/lib/typed/ast/class_statement.rb new file mode 100644 index 00000000..a1f3f8a7 --- /dev/null +++ b/lib/typed/ast/class_statement.rb @@ -0,0 +1,9 @@ +module Typed + class ClassStatement < Statement + attr_accessor :name , :derives , :statements + end + + class ClassField < Statement + attr_accessor :name , :type + end +end diff --git a/lib/typed/ast/code.rb b/lib/typed/ast/code.rb new file mode 100644 index 00000000..58faefc3 --- /dev/null +++ b/lib/typed/ast/code.rb @@ -0,0 +1,27 @@ + +# Base class for Expresssion and Statement +module Typed + + class Code + + end + + class Statement < Code + end + class Expression < Code + end + +end + +require_relative "while_statement" +require_relative "if_statement" +require_relative "return_statement" +require_relative "statements" +require_relative "operator_expression" +require_relative "field_access" +require_relative "call_site" +require_relative "basic_values" +require_relative "assignment" +require_relative "class_statement" +require_relative "function_statement" +require_relative "to_code" diff --git a/lib/typed/ast/field_access.rb b/lib/typed/ast/field_access.rb new file mode 100644 index 00000000..8c520c03 --- /dev/null +++ b/lib/typed/ast/field_access.rb @@ -0,0 +1,5 @@ +module Typed + class FieldAccess < Expression + attr_accessor :receiver , :field + end +end diff --git a/lib/typed/ast/function_statement.rb b/lib/typed/ast/function_statement.rb new file mode 100644 index 00000000..83a1017a --- /dev/null +++ b/lib/typed/ast/function_statement.rb @@ -0,0 +1,5 @@ +module Typed + class FunctionStatement < Statement + attr_accessor :return_type , :name , :parameters, :statements , :receiver + end +end diff --git a/lib/typed/ast/if_statement.rb b/lib/typed/ast/if_statement.rb new file mode 100644 index 00000000..63cc6917 --- /dev/null +++ b/lib/typed/ast/if_statement.rb @@ -0,0 +1,5 @@ +module Typed + class IfStatement < Statement + attr_accessor :branch_type , :condition , :if_true , :if_false + end +end diff --git a/lib/typed/ast/operator_expression.rb b/lib/typed/ast/operator_expression.rb new file mode 100644 index 00000000..4df8bbb9 --- /dev/null +++ b/lib/typed/ast/operator_expression.rb @@ -0,0 +1,5 @@ +module Typed + class OperatorExpression < Expression + attr_accessor :operator , :left_expression , :right_expression + end +end diff --git a/lib/typed/ast/return_statement.rb b/lib/typed/ast/return_statement.rb new file mode 100644 index 00000000..e50b3cc8 --- /dev/null +++ b/lib/typed/ast/return_statement.rb @@ -0,0 +1,5 @@ +module Typed + class ReturnStatement < Statement + attr_accessor :return_value + end +end diff --git a/lib/typed/ast/statements.rb b/lib/typed/ast/statements.rb new file mode 100644 index 00000000..aabe44f9 --- /dev/null +++ b/lib/typed/ast/statements.rb @@ -0,0 +1,5 @@ +module Typed + class Statements < Statement + attr_accessor :statements + end +end diff --git a/lib/typed/ast/to_code.rb b/lib/typed/ast/to_code.rb new file mode 100644 index 00000000..bee48c97 --- /dev/null +++ b/lib/typed/ast/to_code.rb @@ -0,0 +1,163 @@ +module Typed + + def self.ast_to_code statement + compiler = ToCode.new + compiler.process statement + end + + class ToCode < AST::Processor + + def handler_missing node + raise "No handler on_#{node.type}(node)" + end + + def on_class statement + name , derives , statements = *statement + w = ClassStatement.new() + w.name = name + w.derives = derives.children.first + w.statements = process(statements) + w + end + + def on_function statement + return_type , name , parameters, statements , receiver = *statement + w = FunctionStatement.new() + w.return_type = return_type + w.name = name.children.first + w.parameters = parameters.to_a.collect do |p| + raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter + p.children + end + w.statements = process(statements) + w.receiver = receiver + w + end + + def on_field_def statement + type , name , value = *statement + w = FieldDef.new() + w.type = type + w.name = process(name) + w.value = process(value) if value + w + end + + def on_class_field statement + type , name = *statement + w = ClassField.new() + w.type = type + w.name = name + w + end + + def on_while_statement statement + branch_type , condition , statements = *statement + w = WhileStatement.new() + w.branch_type = branch_type + w.condition = process(condition) + w.statements = process(statements) + w + end + + def on_if_statement statement + branch_type , condition , if_true , if_false = *statement + w = IfStatement.new() + w.branch_type = branch_type + w.condition = process(condition) + w.if_true = process(if_true) + w.if_false = process(if_false) + w + end + + def process_first code + raise "Too many children #{code.inspect}" if code.children.length != 1 + process code.children.first + end + alias :on_conditional :process_first + alias :on_condition :process_first + alias :on_field :process_first + + def on_statements statement + w = Statements.new() + return w unless statement.children + return w unless statement.children.first + w.statements = process_all(statement.children) + w + end + alias :on_true_statements :on_statements + alias :on_false_statements :on_statements + + def on_return statement + w = ReturnStatement.new() + w.return_value = process(statement.children.first) + w + end + + def on_operator_value statement + operator , left_e , right_e = *statement + w = OperatorExpression.new() + w.operator = operator + w.left_expression = process(left_e) + w.right_expression = process(right_e) + w + end + + def on_field_access statement + receiver_ast , field_ast = *statement + w = FieldAccess.new() + w.receiver = process(receiver_ast) + w.field = process(field_ast) + w + end + + def on_receiver expression + process expression.children.first + end + + def on_call statement + name_s , arguments , receiver = *statement + w = CallSite.new() + w.name = name_s.children.first + w.arguments = process_all(arguments) + w.receiver = process(receiver) + w + end + + def on_int expression + IntegerExpression.new(expression.children.first) + end + + def on_true expression + TrueExpression.new + end + + def on_false expression + FalseExpression.new + end + + def on_nil expression + NilExpression.new + end + + def on_name statement + NameExpression.new(statement.children.first) + end + def on_string expression + StringExpression.new(expression.children.first) + end + + def on_class_name expression + ClassExpression.new(expression.children.first) + end + + def on_assignment statement + name , value = *statement + w = Assignment.new() + w.name = process name + w.value = process(value) + w + end + + end +end diff --git a/lib/typed/ast/while_statement.rb b/lib/typed/ast/while_statement.rb new file mode 100644 index 00000000..3a83247c --- /dev/null +++ b/lib/typed/ast/while_statement.rb @@ -0,0 +1,5 @@ +module Typed + class WhileStatement < Statement + attr_accessor :branch_type , :condition , :statements + end +end