diff --git a/lib/vm/builtins.rb b/lib/vm/builtins.rb deleted file mode 100644 index 72922596..00000000 --- a/lib/vm/builtins.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'java' -java_import java.lang.System -java_import java.io.PrintStream - -module Vm - module Builtins - def add_builtins - public_static_method 'print', [], int, int do - iload 0 - - getstatic System, :out, PrintStream - swap - invokevirtual PrintStream, :print, [void, int] - - ldc 0 - ireturn - end - - public_static_method 'minus', [], int, int, int do - iload 0 - iload 1 - isub - ireturn - end - end - end -end diff --git a/lib/vm/compiler.rb b/lib/vm/compiler.rb deleted file mode 100644 index 777f36aa..00000000 --- a/lib/vm/compiler.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'bitescript' -require 'vm/parser' -require 'vm/transform' -require 'vm/builtins' - -module Vm - class Compiler - def initialize(filename) - @filename = filename - @classname = File.basename(@filename, '.vm') - end - - def compile - tree = parse_source - funcs, exprs = split_functions tree - classname = @classname - - builder = BiteScript::FileBuilder.build(@filename) do - public_class classname, object do |klass| - klass.extend(Builtins) - klass.add_builtins - - funcs.each do |f| - context = Hash.new - f.eval(context, klass) - end - - klass.public_static_method 'main', [], void, string[] do |method| - context = Hash.new - exprs.each do |e| - e.eval(context, method) - end - - method.returnvoid - end - end - end - - write_result builder - end - - private - - def parse_source - source = File.expand_path(@filename) - program = IO.read source - - parser = Parser.new - transform = Transform.new - syntax = parser.parse(program) - tree = transform.apply(syntax) - - Array(tree) - end - - def split_functions(tree) - first_expr = tree.index { |t| ! t.is_a?(FunctionExpression) } - funcs = first_expr ? tree[0...first_expr] : tree - exprs = first_expr ? tree[first_expr..-1] : [] - - [funcs, exprs] - end - - def write_result(builder) - destination = File.expand_path(@classname + '.class') - - builder.generate do |n, b| - File.open(destination, 'wb') do |f| - f.write b.generate - end - end - end - end -end