Class: AjLisp::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ajlisp/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(lexer) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/ajlisp/parser.rb', line 6

def initialize(lexer)
    @lexer = lexer
end

Instance Method Details

#parseExpressionObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ajlisp/parser.rb', line 10

def parseExpression
    token = @lexer.nextToken
    
    if token == nil
        return nil
    end
    
    if token.type == TokenType::ATOM
        if token.value[0,1] == "."
            return DotVerbAtom.new token.value
        end
        
        if token.value[0,1] == "@"
            return AtConstantAtom.new token.value
        end
        
        if token.value[0,1] == "'"
            return List.make [NamedAtom.new("quote"), parseExpression]
        end
        
        if token.value[0,1] == "`"
            return List.make [NamedAtom.new("backquote"), parseExpression]
        end

        if token.value[0,1] == ","
            return List.make [NamedAtom.new("unquote"), parseExpression]
        end
        
        if token.value == "false"
            return false
        end
        
        if token.value == "true"
            return true
        end
        
        if token.value == "nil"
            return NilAtom.instance
        end

        return NamedAtom.new token.value
    end
    
    if token.type == TokenType::INTEGER
        return token.value.to_i
    end
    
    if token.type == TokenType::STRING
        return token.value
    end
    
    if token.type == TokenType::SEPARATOR and token.value == "("
        elements = []
        token = @lexer.nextToken
        
        while not (token.type == TokenType::SEPARATOR and token.value == ")")
            @lexer.pushToken token
            elements.push parseExpression
            token = @lexer.nextToken
        end

        if elements.length == 0
            return EmptyList.instance
        else
            return List::make elements
        end
    end
end