Compare commits

...

8 Commits

Author SHA1 Message Date
Torsten Ruger 56a02bd6ea use ast that produces ruby output 2016-12-08 19:34:59 +02:00
Torsten Ruger f5b54c5fca add cli for soml->s-exp 2016-12-08 18:54:27 +02:00
Torsten Ruger 5c03db709f fix if false logic 2016-03-07 11:51:58 +02:00
Torsten Ruger 324c9c2eae better handling of statement lists 2016-03-07 11:32:28 +02:00
Torsten Ruger c9d25fcf81 easier constructor 2016-03-07 11:32:09 +02:00
Torsten Ruger f51ed376c4 add missing as types 2016-03-06 14:07:25 +02:00
Torsten Ruger 4a9b492dd9 move to typed structure
on the way to removing the language, the at needs to be replaced with a
typed structure.
2016-03-06 09:40:41 +02:00
Torsten Ruger 9ca03ef115 hopefully fix travis issue 2015-11-30 20:07:11 +02:00
25 changed files with 343 additions and 49 deletions

View File

@ -5,9 +5,8 @@ gem "rake"
gem "soml-parser" , :path => "."
# use this for debugging, the printout is better (cut/paste)
gem "ast" , :github => "dancinglightning/ast" , :branch => :new_inspect
#gem "ast"
gem "ast" , :github => "dancinglightning/ast"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do

View File

@ -1,7 +1,6 @@
GIT
remote: git://github.com/dancinglightning/ast.git
revision: a9bc29377a94d2194e92596aceb9be7c7db4c8f8
branch: new_inspect
revision: 3814fb102af82a30bf334005ebe17332744f9dad
specs:
ast (2.1.0)
@ -41,4 +40,4 @@ DEPENDENCIES
soml-parser!
BUNDLED WITH
1.10.6
1.13.5

View File

@ -2,3 +2,4 @@ require 'parslet'
require 'parser/soml'
require "ast"
require 'parser/transform'
require "soml/code/code"

View File

@ -0,0 +1,13 @@
module Soml
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

View File

@ -0,0 +1,39 @@
module Soml
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

View File

@ -0,0 +1,5 @@
module Soml
class CallSite < Expression
attr_accessor :name , :receiver , :arguments
end
end

View File

@ -0,0 +1,9 @@
module Soml
class ClassStatement < Statement
attr_accessor :name , :derives , :statements
end
class ClassField < Statement
attr_accessor :name , :type
end
end

27
lib/soml/code/code.rb Normal file
View File

@ -0,0 +1,27 @@
# Base class for Expresssion and Statement
module Soml
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"

View File

@ -0,0 +1,5 @@
module Soml
class FieldAccess < Expression
attr_accessor :receiver , :field
end
end

View File

@ -0,0 +1,5 @@
module Soml
class FunctionStatement < Statement
attr_accessor :return_type , :name , :parameters, :statements , :receiver
end
end

View File

@ -0,0 +1,5 @@
module Soml
class IfStatement < Statement
attr_accessor :branch_type , :condition , :if_true , :if_false
end
end

View File

@ -0,0 +1,5 @@
module Soml
class OperatorExpression < Expression
attr_accessor :operator , :left_expression , :right_expression
end
end

View File

@ -0,0 +1,5 @@
module Soml
class ReturnStatement < Statement
attr_accessor :return_value
end
end

View File

@ -0,0 +1,5 @@
module Soml
class Statements < Statement
attr_accessor :statements
end
end

163
lib/soml/code/to_code.rb Normal file
View File

@ -0,0 +1,163 @@
module Soml
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

View File

@ -0,0 +1,5 @@
module Soml
class WhileStatement < Statement
attr_accessor :branch_type , :condition , :statements
end
end

View File

@ -8,8 +8,10 @@ to run just a single, replace all with what you want to test. Minitest accept a
ruby test/test_class.rb -n test_class_ops_parse
Notice tough the "_parse" at the end, while you will find no such function. The Magic (explained below) renerates three
functions per case. Your options are "_parse" , "_transform" , or if it's really bad, "_ast" (this should really work when the previous two work)
Notice tough the "_parse" at the end, while you will find no such function.
The Magic (explained below) generates three functions per case.
Your options are "_parse" , "_transform" , or if it's really bad, "_ast"
(this should really work when the previous two work)
### Directories
@ -26,7 +28,7 @@ Apart from just plain more tests, two additional directories are planned. One is
Parsing is a two step process with parslet:
- parse takes an input and outputs hashes/arrays with basic types
- tramsform takes the output of parse and generates an ast (as specified by the transformation)
- transform takes the output of parse and generates an ast (as specified by the transformation)
A test tests both phases separately and again together.
Each test must thus specify (as instance variables):

View File

@ -1,5 +0,0 @@
[42, foo]
-- -- --
s(:array,
s(:int, 42),
s(:name, :foo))

View File

@ -1,10 +0,0 @@
[ 3 + 4 , foo(22) ]
-- -- --
s(:array,
s(:operator_value, :+,
s(:int, 3),
s(:int, 4)),
s(:call,
s(:name, :foo),
s(:arguments,
s(:int, 22))))

View File

@ -1,4 +0,0 @@
[42]
-- -- --
s(:array,
s(:int, 42))

View File

@ -1,6 +0,0 @@
{ foo => 33 }
-- -- --
s(:hash,
s(:assoc,
s(:name, :foo),
s(:int, 33)))

View File

@ -1,6 +0,0 @@
{ foo => true }
-- -- --
s(:hash,
s(:assoc,
s(:name, :foo),
s(:true)))

View File

@ -1,9 +0,0 @@
{foo => 33 , bar => 42}
-- -- --
s(:hash,
s(:assoc,
s(:name, :foo),
s(:int, 33)),
s(:assoc,
s(:name, :bar),
s(:int, 42)))

View File

@ -34,6 +34,12 @@ class TestAll < MiniTest::Test
end
end
assert_equal sexp , result
check_transform sexp
end
def check_transform sexp
code = Soml.ast_to_code sexp
assert code.is_a?(Soml::Code) , "Returned #{code}"
end
# this creates test methods dynamically. For each test_* method we create

36
to_code.rb Normal file
View File

@ -0,0 +1,36 @@
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :development)
require 'soml-parser'
require "parslet/convenience"
require "ast/sexp"
require "pp"
class Converter
include AST::Sexp
SEPERATOR = "-- -- --"
def to_ast
file = ARGV[0]
inn = File.new(file).read.split(SEPERATOR).first
begin
syntax = Parser::Salama.new.parse(inn)
rescue
root = file.split("/").last.split(".").first
parser = Parser::Salama.new.send root.to_sym
syntax = parser.parse_with_debug(inn )
end
result = Parser::Transform.new.apply(syntax)
out_file = File.new(file, "w")
out_file.puts inn
out_file.puts SEPERATOR
out_file.puts result.inspect
out_file.close
end
end
Converter.new.to_ast