-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
153 lines (142 loc) · 3.95 KB
/
functions.go
1
2
3
4
5
6
7
8
9
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package php
import (
"github.com/stephens2424/php/ast"
"github.com/stephens2424/php/lexer"
"github.com/stephens2424/php/token"
)
func (p *Parser) parseFunctionStmt(inMethod bool) *ast.FunctionStmt {
stmt := &ast.FunctionStmt{}
stmt.FunctionDefinition = p.parseFunctionDefinition()
if !inMethod {
p.namespace.Functions[stmt.Name] = stmt
}
p.scope = ast.NewScope(p.scope, p.FileSet.GlobalScope, p.FileSet.SuperGlobalScope)
stmt.Body = p.parseBlock()
p.scope = p.scope.EnclosingScope
return stmt
}
func (p *Parser) parseFunctionDefinition() *ast.FunctionDefinition {
def := &ast.FunctionDefinition{}
if p.peek().Typ == token.AmpersandOperator {
// This is a function returning a reference ... ignore this for now
p.next()
}
if !p.accept(token.Identifier) {
p.next()
if !lexer.IsKeyword(p.current.Typ, p.current.Val) {
p.errorf("bad function name: %s", p.current.Val)
}
}
def.Name = p.current.Val
def.Arguments = make([]*ast.FunctionArgument, 0)
p.expect(token.OpenParen)
if p.peek().Typ == token.CloseParen {
p.expect(token.CloseParen)
return def
}
def.Arguments = append(def.Arguments, p.parseFunctionArgument())
for {
switch p.peek().Typ {
case token.Comma:
p.expect(token.Comma)
def.Arguments = append(def.Arguments, p.parseFunctionArgument())
case token.CloseParen:
p.expect(token.CloseParen)
return def
default:
p.errorf("unexpected argument separator: %s", p.current)
return def
}
}
}
func (p *Parser) parseFunctionArgument() *ast.FunctionArgument {
arg := &ast.FunctionArgument{}
switch p.peek().Typ {
case token.Identifier, token.Array, token.Self:
p.next()
arg.TypeHint = p.current.Val
}
if p.peek().Typ == token.AmpersandOperator {
p.next()
}
p.expect(token.VariableOperator)
p.next()
arg.Variable = ast.NewVariable(p.current.Val)
if p.peek().Typ == token.AssignmentOperator {
p.expect(token.AssignmentOperator)
p.next()
arg.Default = p.parseExpression()
}
return arg
}
func (p *Parser) parseFunctionCall(callable ast.Expression) *ast.FunctionCallExpression {
expr := &ast.FunctionCallExpression{}
expr.FunctionName = callable
return p.parseFunctionArguments(expr)
}
func (p *Parser) parseFunctionArguments(expr *ast.FunctionCallExpression) *ast.FunctionCallExpression {
expr.Arguments = make([]ast.Expression, 0)
p.expect(token.OpenParen)
if p.peek().Typ == token.CloseParen {
p.expect(token.CloseParen)
return expr
}
expr.Arguments = append(expr.Arguments, p.parseNextExpression())
for p.peek().Typ != token.CloseParen {
p.expect(token.Comma)
arg := p.parseNextExpression()
if arg == nil {
break
}
expr.Arguments = append(expr.Arguments, arg)
}
p.expect(token.CloseParen)
return expr
}
func (p *Parser) parseAnonymousFunction() ast.Expression {
f := &ast.AnonymousFunction{}
f.Arguments = make([]*ast.FunctionArgument, 0)
f.ClosureVariables = make([]*ast.FunctionArgument, 0)
p.expect(token.OpenParen)
if p.peek().Typ != token.CloseParen {
f.Arguments = append(f.Arguments, p.parseFunctionArgument())
}
Loop:
for {
switch p.peek().Typ {
case token.Comma:
p.expect(token.Comma)
f.Arguments = append(f.Arguments, p.parseFunctionArgument())
case token.CloseParen:
break Loop
default:
p.errorf("unexpected argument separator: %s", p.current)
return f
}
}
p.expect(token.CloseParen)
// Closure variables
if p.peek().Typ == token.Use {
p.expect(token.Use)
p.expect(token.OpenParen)
f.ClosureVariables = append(f.ClosureVariables, p.parseFunctionArgument())
ClosureLoop:
for {
switch p.peek().Typ {
case token.Comma:
p.expect(token.Comma)
f.ClosureVariables = append(f.ClosureVariables, p.parseFunctionArgument())
case token.CloseParen:
break ClosureLoop
default:
p.errorf("unexpected argument separator: %s", p.current)
return f
}
}
p.expect(token.CloseParen)
}
p.scope = ast.NewScope(p.scope, p.FileSet.GlobalScope, p.FileSet.SuperGlobalScope)
f.Body = p.parseBlock()
p.scope = p.scope.EnclosingScope
return f
}