Skip to content

Commit 71daf5b

Browse files
authored
Merge pull request #30 from haya14busa/paren-expr
Add ast.ParenExpr
2 parents be58314 + a395096 commit 71daf5b

File tree

11 files changed

+66
-5
lines changed

11 files changed

+66
-5
lines changed

ast/node.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,15 @@ type LambdaExpr struct {
526526

527527
func (i *LambdaExpr) Pos() Pos { return i.Lcurlybrace }
528528

529+
// ParenExpr node represents a parenthesized expression.
530+
// vimlparsr: PARENEXPR .value
531+
type ParenExpr struct {
532+
Lparen Pos // position of "("
533+
X Expr // parenthesized expression
534+
}
535+
536+
func (i *ParenExpr) Pos() Pos { return i.Lparen }
537+
529538
// stmtNode() ensures that only ExComamnd and Comment nodes can be assigned to
530539
// an Statement.
531540
//
@@ -577,3 +586,4 @@ func (*CurlyNameLit) exprNode() {}
577586
func (*CurlyNameExpr) exprNode() {}
578587
func (*Ident) exprNode() {}
579588
func (*LambdaExpr) exprNode() {}
589+
func (*ParenExpr) exprNode() {}

ast/walk.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ func Walk(v Visitor, node Node) {
198198
case *LambdaExpr:
199199
walkIdentList(v, n.Params)
200200

201+
case *ParenExpr:
202+
Walk(v, n.X)
203+
201204
default:
202205
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
203206
}

autoload/vimlparser.vim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ let s:NODE_REG = 89
137137
let s:NODE_CURLYNAMEPART = 90
138138
let s:NODE_CURLYNAMEEXPR = 91
139139
let s:NODE_LAMBDA = 92
140+
let s:NODE_PARENEXPR = 93
140141

141142
let s:TOKEN_EOF = 1
142143
let s:TOKEN_EOL = 2
@@ -403,6 +404,7 @@ endfunction
403404
" CURLYNAMEPART .value
404405
" CURLYNAMEEXPR .value
405406
" LAMBDA .rlist .left
407+
" PARENEXPR .value
406408
function! s:Node(type)
407409
return {'type': a:type}
408410
endfunction
@@ -3514,7 +3516,9 @@ function! s:ExprParser.parse_expr9()
35143516
endwhile
35153517
return node
35163518
elseif token.type == s:TOKEN_POPEN
3517-
let node = self.parse_expr1()
3519+
let node = s:Node(s:NODE_PARENEXPR)
3520+
let node.pos = token.pos
3521+
let node.value = self.parse_expr1()
35183522
let token = self.tokenizer.get()
35193523
if token.type != s:TOKEN_PCLOSE
35203524
throw s:Err(printf('unexpected token: %s', token.value), token.pos)
@@ -4185,6 +4189,8 @@ function! s:Compiler.compile(node)
41854189
return self.compile_curlynameexpr(a:node)
41864190
elseif a:node.type == s:NODE_LAMBDA
41874191
return self.compile_lambda(a:node)
4192+
elseif a:node.type == s:NODE_PARENEXPR
4193+
return self.compile_parenexpr(a:node)
41884194
else
41894195
throw printf('Compiler: unknown node: %s', string(a:node))
41904196
endif
@@ -4648,6 +4654,10 @@ function! s:Compiler.compile_lambda(node)
46484654
return printf('(lambda (%s) %s)', join(rlist, ' '), self.compile(a:node.left))
46494655
endfunction
46504656

4657+
function! s:Compiler.compile_parenexpr(node)
4658+
return self.compile(a:node.value)
4659+
endfunction
4660+
46514661
" TODO: under construction
46524662
let s:RegexpParser = {}
46534663

compiler/compiler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ func (c *Compiler) compileExpr(node ast.Expr) string {
437437
params = append(params, p.Name)
438438
}
439439
return fmt.Sprintf("(lambda (%s) %s)", strings.Join(params, " "), c.compileExpr(n.Expr))
440+
case *ast.ParenExpr:
441+
return c.compileExpr(n.X)
440442
}
441443
return ""
442444
}

go/export.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ func newAstNode(n *VimNode, filename string) ast.Node {
446446
Expr: newExprNode(n.left, filename),
447447
}
448448

449+
case NODE_PARENEXPR:
450+
n := n.value.(*VimNode)
451+
return &ast.ParenExpr{
452+
Lparen: pos,
453+
X: newExprNode(n, filename),
454+
}
455+
449456
}
450457
panic(fmt.Errorf("Unknown node type: %v, node: %v", n.type_, n))
451458
}

go/gocompiler.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ call extend(s:, vimlparser#import())
66

77
let s:opprec = {}
88
let s:opprec[s:NODE_TERNARY] = 1
9+
let s:opprec[s:NODE_PARENEXPR] = 1
910
let s:opprec[s:NODE_OR] = 2
1011
let s:opprec[s:NODE_AND] = 3
1112
let s:opprec[s:NODE_EQUAL] = 4
@@ -280,6 +281,8 @@ function s:GoCompiler.compile(node)
280281
return self.compile_env(a:node)
281282
elseif a:node.type == s:NODE_REG
282283
return self.compile_reg(a:node)
284+
elseif a:node.type == s:NODE_PARENEXPR
285+
return self.compile_parenexpr(a:node)
283286
else
284287
throw self.err('Compiler: unknown node: %s', string(a:node))
285288
endif
@@ -358,6 +361,7 @@ function s:GoCompiler.compile_function(node)
358361
\ || name == 'compile_list'
359362
\ || name == 'compile_curlyname'
360363
\ || name == 'compile_dict'
364+
\ || name == 'compile_parenexpr'
361365
\ ))
362366
return
363367
endif
@@ -956,6 +960,10 @@ function s:GoCompiler.compile_reg(node)
956960
throw 'NotImplemented: reg'
957961
endfunction
958962

963+
function s:GoCompiler.compile_parenexpr(node)
964+
return self.compile(a:node.value)
965+
endfunction
966+
959967
function s:GoCompiler.compile_op1(node, op)
960968
let left = self.compile(a:node.left)
961969
if s:opprec[a:node.type] > s:opprec[a:node.left.type]

go/type.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ func (self *Compiler) compile_dict(n *VimNode) string {
310310
}
311311
}
312312

313+
func (self *Compiler) compile_parenexpr(n *VimNode) string {
314+
return self.compile(n.value.(*VimNode)).(string)
315+
}
316+
313317
type ParseError struct {
314318
Offset int
315319
Line int

go/typedefs.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ call extend(s:typedefs.func, {
318318
\ 'Compiler.compile_curlynamepart': { 'in': ['*VimNode'], 'out': ['string'] },
319319
\ 'Compiler.compile_curlynameexpr': { 'in': ['*VimNode'], 'out': ['string'] },
320320
\ 'Compiler.compile_lambda': { 'in': ['*VimNode'], 'out': ['string'] },
321+
\ 'Compiler.compile_parenexpr': { 'in': ['*VimNode'], 'out': ['string'] },
321322
\ })
322323

323324
function! ImportTypedefs() abort

go/vimlparser.go

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_parenexpr.ok

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; test_parenexpr
2+
(echo 1)
3+
(echo 1)
4+
(echo (+ (+ 1 1) 2))
5+
(echo (+ (+ 1 1) (* 2 (+ 3 4))))

0 commit comments

Comments
 (0)