added node types
This commit is contained in:
parent
60e9d73bc8
commit
46cbe8e952
1 changed files with 39 additions and 7 deletions
46
src/sexpr.cr
46
src/sexpr.cr
|
@ -5,8 +5,40 @@ module Sexpr
|
||||||
|
|
||||||
VERSION = {{ `shards version`.stringify.chomp }}
|
VERSION = {{ `shards version`.stringify.chomp }}
|
||||||
|
|
||||||
alias Node = String | Body
|
module AST
|
||||||
alias Body = Array(String) | Array(Node)
|
abstract struct Node
|
||||||
|
property value
|
||||||
|
|
||||||
|
def initialize(@value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
struct Body < Node
|
||||||
|
property value : Array(Node)
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@value = [] of Node
|
||||||
|
end
|
||||||
|
|
||||||
|
def <<(other : Node) : self
|
||||||
|
@value << other
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def <<(other : self) : self
|
||||||
|
self << other.value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
struct SymNode < Node
|
||||||
|
property value : String
|
||||||
|
end
|
||||||
|
|
||||||
|
struct StringNode < Node
|
||||||
|
property value : String
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private class Wrapper
|
private class Wrapper
|
||||||
private property i = 0
|
private property i = 0
|
||||||
|
@ -44,8 +76,8 @@ module Sexpr
|
||||||
@i -= 1
|
@i -= 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse : Body
|
def parse : AST::Body
|
||||||
body = [] of Node
|
body = AST::Body.new
|
||||||
|
|
||||||
while char = get_char
|
while char = get_char
|
||||||
if char == ')'
|
if char == ')'
|
||||||
|
@ -53,10 +85,10 @@ module Sexpr
|
||||||
elsif char == '('
|
elsif char == '('
|
||||||
body << parse
|
body << parse
|
||||||
elsif char == '"'
|
elsif char == '"'
|
||||||
body << read_value(Terms::STRING)
|
body << AST::StringNode.new(read_value(Terms::STRING))
|
||||||
elsif !Terms::SPACE.matches?(char.to_s)
|
elsif !Terms::SPACE.matches?(char.to_s)
|
||||||
un_get_char
|
un_get_char
|
||||||
body << read_value(Terms::LIST)
|
body.value << AST::SymNode.new(read_value(Terms::LIST))
|
||||||
un_get_char
|
un_get_char
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -69,7 +101,7 @@ module Sexpr
|
||||||
source.gsub(/;;?.+/, nil)
|
source.gsub(/;;?.+/, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(source : String) : Body
|
def parse(source : String) : AST::Body
|
||||||
Wrapper.new(remove_comments(source)).parse
|
Wrapper.new(remove_comments(source)).parse
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue