2018-07-25 14:04:27 -07:00
|
|
|
require "./print_visitor.cr"
|
|
|
|
|
|
2018-07-24 17:30:10 -07:00
|
|
|
module Chalk
|
2018-07-25 13:53:35 -07:00
|
|
|
class Visitor
|
|
|
|
|
def visit(tree : Tree)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def finish(tree : Tree)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class Tree
|
|
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
v.finish(self)
|
|
|
|
|
end
|
2018-07-25 14:04:27 -07:00
|
|
|
|
|
|
|
|
def to_s(io)
|
|
|
|
|
accept(PrintVisitor.new io)
|
|
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeId < Tree
|
|
|
|
|
property id : String
|
|
|
|
|
|
|
|
|
|
def initialize(@id : String)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeLit < Tree
|
|
|
|
|
property lit : Int64
|
|
|
|
|
|
|
|
|
|
def initialize(@lit : Int64)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeCall < Tree
|
|
|
|
|
property name : String
|
|
|
|
|
property params : Array(Tree)
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@name : String, @params : Array(Tree))
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
|
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@params.each &.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
class TreeOp < Tree
|
|
|
|
|
property op : TokenType
|
|
|
|
|
property left : Tree
|
|
|
|
|
property right : Tree
|
|
|
|
|
|
|
|
|
|
def initialize(@op : TokenType, @left : Tree, @right : Tree)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
|
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@left.accept(v)
|
|
|
|
|
@right.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
class TreeBlock < Tree
|
|
|
|
|
def initialize(@children : Array(Tree))
|
|
|
|
|
end
|
2018-07-24 23:57:20 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@children.each &.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 23:57:20 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeFunction < Tree
|
|
|
|
|
property name : String
|
|
|
|
|
property params : Array(String)
|
|
|
|
|
property block : Tree
|
2018-07-24 23:57:20 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@name : String, @params : Array(String), @block : Tree)
|
|
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@block.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeVar < Tree
|
|
|
|
|
property name : String
|
|
|
|
|
property expr : Tree
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@name : String, @expr : Tree)
|
|
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@expr.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
class TreeAssign < Tree
|
|
|
|
|
property name : String
|
|
|
|
|
property expr : Tree
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@name : String, @expr : Tree)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
|
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@expr.accept(v)
|
|
|
|
|
v.finish(self)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeIf < Tree
|
|
|
|
|
property condition : Tree
|
|
|
|
|
property block : Tree
|
|
|
|
|
property otherwise : Tree?
|
|
|
|
|
|
|
|
|
|
def initialize(@condition : Tree, @block : Tree, @otherwise : Tree? = nil)
|
|
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@condition.accept(v)
|
|
|
|
|
@block.accept(v)
|
|
|
|
|
@otherwise.try &.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class TreeWhile < Tree
|
|
|
|
|
property condition : Tree
|
|
|
|
|
property block : Tree
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@condition : Tree, @block : Tree)
|
|
|
|
|
end
|
2018-07-24 23:57:20 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@condition.accept(v)
|
|
|
|
|
@block.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 23:57:20 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
2018-07-24 23:57:20 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
class TreeReturn < Tree
|
|
|
|
|
property rvalue : Tree
|
2018-07-24 17:30:10 -07:00
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def initialize(@rvalue : Tree)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
|
|
|
|
|
2018-07-25 13:53:35 -07:00
|
|
|
def accept(v : Visitor)
|
|
|
|
|
v.visit(self)
|
|
|
|
|
@rvalue.accept(v)
|
|
|
|
|
v.finish(self)
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|
2018-07-25 13:53:35 -07:00
|
|
|
end
|
2018-07-24 17:30:10 -07:00
|
|
|
end
|