Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: README.md
- Loading branch information
Showing
15 changed files
with
189 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
xa := xa + 4; | ||
xb := xb + 3; | ||
|
||
loop xb do | ||
xa := xa + 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'gen/symboltable' | ||
|
||
class JSVisitor | ||
|
||
def initialize | ||
@target = "" | ||
@loop_counter = 0 | ||
end | ||
|
||
def visit_start(node) | ||
compile "var sys = require('sys');" | ||
SymbolTable.identifiers.each do |token| | ||
compile "#{token.name} = 0;" | ||
end | ||
end | ||
|
||
def visit_assignment(node) | ||
compile "#{node.lvalue.name} = #{node.op1.name} #{node.op.op} #{node.op2.number}; " | ||
end | ||
|
||
def visit_loop_start(node) | ||
compile "_loopTerminate#{@loop_counter} = #{node.to.name};" | ||
compile "for(var _loopVar#{@loop_counter} = 0; _loopVar#{@loop_counter} < _loopTerminate#{@loop_counter}; _loopVar#{@loop_counter}++) {" | ||
@loop_counter += 1 | ||
end | ||
|
||
def visit_loop_end(node) | ||
compile "}" | ||
end | ||
|
||
def run | ||
compile "sys.puts(\"xa = \" + xa);" | ||
nodejs = IO.popen("/usr/local/bin/node", "r+") | ||
nodejs.puts @target | ||
nodejs.close_write | ||
puts nodejs.gets | ||
end | ||
|
||
|
||
private | ||
|
||
def compile(source) | ||
#p source | ||
@target += "#{source}" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class SymbolTable | ||
|
||
@@symbols = [] | ||
@@identifiers = [] | ||
|
||
def self.getVar(symbol) | ||
@@symbols[symbol] || 0 | ||
end | ||
|
||
def self.addVar(symbol, value = 0) | ||
@@symbols[symbol] = value | ||
end | ||
|
||
def self.identifiers; @@identifiers; end; | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
a := a + 3; | ||
b := b + 4; | ||
|
||
loop a do | ||
loop b do | ||
xa := xa + 1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
class NodeAssignment | ||
attr_accessor :lvalue, :op1, :op, :op2 | ||
|
||
def accept(visitor) | ||
visitor.visit_assignment self | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class NodeLoop < ParentNode | ||
attr_accessor :to | ||
|
||
def accept(visitor) | ||
visitor.visit_loop_start self | ||
stmts.each do |stmt| | ||
stmt.accept visitor | ||
end | ||
visitor.visit_loop_end self | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class ParentNode | ||
|
||
attr_accessor :stmts | ||
|
||
def initialize | ||
@stmts = [] | ||
end | ||
|
||
def <<(it) | ||
@stmts << it | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class NodeStart < ParentNode | ||
def accept(visitor) | ||
visitor.visit_start self | ||
stmts.each do |stmt| | ||
stmt.accept visitor | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
xa := xa + 7; | ||
xb := xb + 3; | ||
|
||
loop xb do | ||
xa := xa - 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Token; end; | ||
|
||
class TIdentifier < Token | ||
attr_accessor :name | ||
def initialize(name) | ||
@name = name | ||
end | ||
end | ||
|
||
class TNumber < Token | ||
attr_accessor :number | ||
def initialize(number) | ||
@number = number | ||
end | ||
end | ||
|
||
class TBinOp < Token | ||
attr_accessor :op | ||
def initialize(op) | ||
@op = op | ||
end | ||
end | ||
|
||
class TColon < Token; end; | ||
class TEq < Token; end; | ||
class TTerminate < Token; end; | ||
class TEnd < Token; end; | ||
class TLoop < Token; end; | ||
class TDo < Token; end; | ||
class TSemicolon < Token; end; |