Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope hack for compiler #55

Merged
merged 2 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions autoload/vimlparser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,10 @@ function! s:VimLParser.parse_command_modifiers()
let modifiers = []
while s:TRUE
let pos = self.reader.tell()
let d = ''
if s:isdigit(self.reader.peekn(1))
let d = self.reader.read_digit()
call self.reader.skip_white()
else
let d = ''
endif
let k = self.reader.read_alpha()
let c = self.reader.peekn(1)
Expand Down Expand Up @@ -802,6 +801,7 @@ endfunction

function! s:VimLParser.find_command()
let c = self.reader.peekn(1)
let name = ''

if c ==# 'k'
call self.reader.getn(1)
Expand Down Expand Up @@ -989,6 +989,7 @@ endfunction

" TODO:
function! s:VimLParser.parse_cmd_common()
let end = self.reader.getpos()
if self.ea.cmd.flags =~# '\<TRLBAR\>' && !self.ea.usefilter
let end = self.separate_nextcmd()
elseif self.ea.cmd.name ==# '!' || self.ea.cmd.name ==# 'global' || self.ea.cmd.name ==# 'vglobal' || self.ea.usefilter
Expand Down Expand Up @@ -1134,6 +1135,7 @@ function! s:VimLParser.parse_cmd_loadkeymap()
endfunction

function! s:VimLParser.parse_cmd_lua()
let lines = []
call self.reader.skip_white()
if self.reader.peekn(2) ==# '<<'
call self.reader.getn(2)
Expand Down Expand Up @@ -1254,6 +1256,7 @@ function! s:VimLParser.parse_cmd_function()
else
let named = {}
while s:TRUE
let varnode = s:Node(s:NODE_IDENTIFIER)
let token = tokenizer.get()
if token.type == s:TOKEN_IDENTIFIER
if !s:isargname(token.value) || token.value ==# 'firstline' || token.value ==# 'lastline'
Expand All @@ -1262,7 +1265,6 @@ function! s:VimLParser.parse_cmd_function()
throw s:Err(printf('E853: Duplicate argument name: %s', token.value), token.pos)
endif
let named[token.value] = 1
let varnode = s:Node(s:NODE_IDENTIFIER)
let varnode.pos = token.pos
let varnode.value = token.value
call add(node.rlist, varnode)
Expand All @@ -1283,7 +1285,6 @@ function! s:VimLParser.parse_cmd_function()
throw s:Err(printf('unexpected token: %s', token.value), token.pos)
endif
elseif token.type == s:TOKEN_DOTDOTDOT
let varnode = s:Node(s:NODE_IDENTIFIER)
let varnode.pos = token.pos
let varnode.value = token.value
call add(node.rlist, varnode)
Expand Down Expand Up @@ -2672,6 +2673,7 @@ function! s:ExprTokenizer.get2()
" @<EOL> is treated as @"
return self.token(s:TOKEN_REG, r.getn(2), pos)
elseif c ==# '&'
let s = ''
if (r.p(1) ==# 'g' || r.p(1) ==# 'l') && r.p(2) ==# ':'
let s = r.getn(3) . r.read_word()
else
Expand Down Expand Up @@ -3158,6 +3160,7 @@ function! s:ExprParser.parse_expr8()
if token.type != s:TOKEN_SQCLOSE
throw s:Err(printf('unexpected token: %s', token.value), token.pos)
endif
let left = node
else
let right = self.parse_expr1()
if self.tokenizer.peek().type == s:TOKEN_COLON
Expand All @@ -3174,6 +3177,7 @@ function! s:ExprParser.parse_expr8()
if token.type != s:TOKEN_SQCLOSE
throw s:Err(printf('unexpected token: %s', token.value), token.pos)
endif
let left = node
else
let node = s:Node(s:NODE_SUBSCRIPT)
let node.pos = npos
Expand All @@ -3183,9 +3187,9 @@ function! s:ExprParser.parse_expr8()
if token.type != s:TOKEN_SQCLOSE
throw s:Err(printf('unexpected token: %s', token.value), token.pos)
endif
let left = node
endif
endif
let left = node
unlet node
elseif token.type == s:TOKEN_POPEN
let node = s:Node(s:NODE_CALL)
Expand Down Expand Up @@ -3250,6 +3254,7 @@ endfunction
function! s:ExprParser.parse_expr9()
let pos = self.reader.tell()
let token = self.tokenizer.get()
let node = s:Node(-1)
if token.type == s:TOKEN_NUMBER
let node = s:Node(s:NODE_NUMBER)
let node.pos = token.pos
Expand Down Expand Up @@ -3476,12 +3481,13 @@ function! s:ExprParser.parse_identifier()
let node = s:Node(s:NODE_IDENTIFIER)
let node.pos = npos
let node.value = curly_parts[0].value
return node
else
let node = s:Node(s:NODE_CURLYNAME)
let node.pos = npos
let node.value = curly_parts
endif
return node
endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add indent space into previous line?

endfunction

function! s:ExprParser.parse_curly_parts()
Expand Down Expand Up @@ -3544,6 +3550,7 @@ function! s:LvalueParser.parse_lv8()
let token = self.tokenizer.get()
if !s:iswhite(c) && token.type == s:TOKEN_SQOPEN
let npos = token.pos
let node = s:Node(-1)
if self.tokenizer.peek().type == s:TOKEN_COLON
call self.tokenizer.get()
let node = s:Node(s:NODE_SLICE)
Expand Down Expand Up @@ -3611,6 +3618,7 @@ endfunction
function! s:LvalueParser.parse_lv9()
let pos = self.reader.tell()
let token = self.tokenizer.get()
let node = s:Node(-1)
if token.type == s:TOKEN_COPEN
call self.reader.seek_set(pos)
let node = self.parse_identifier()
Expand Down Expand Up @@ -4134,6 +4142,7 @@ function! s:Compiler.compile_excall(node)
endfunction

function! s:Compiler.compile_let(node)
let left = ''
if a:node.left isnot s:NIL
let left = self.compile(a:node.left)
else
Expand Down Expand Up @@ -4201,6 +4210,7 @@ function! s:Compiler.compile_while(node)
endfunction

function! s:Compiler.compile_for(node)
let left = ''
if a:node.left isnot s:NIL
let left = self.compile(a:node.left)
else
Expand Down
23 changes: 16 additions & 7 deletions js/vimlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,11 @@ VimLParser.prototype.parse_command_modifiers = function() {
var modifiers = [];
while (TRUE) {
var pos = this.reader.tell();
var d = "";
if (isdigit(this.reader.peekn(1))) {
var d = this.reader.read_digit();
this.reader.skip_white();
}
else {
var d = "";
}
var k = this.reader.read_alpha();
var c = this.reader.peekn(1);
this.reader.skip_white();
Expand Down Expand Up @@ -1024,6 +1022,7 @@ VimLParser.prototype.parse_command = function() {

VimLParser.prototype.find_command = function() {
var c = this.reader.peekn(1);
var name = "";
if (c == "k") {
this.reader.getn(1);
var name = "k";
Expand Down Expand Up @@ -1227,6 +1226,7 @@ VimLParser.prototype.parse_cmd_modifier_range = function() {

// TODO:
VimLParser.prototype.parse_cmd_common = function() {
var end = this.reader.getpos();
if (viml_eqregh(this.ea.cmd.flags, "\\<TRLBAR\\>") && !this.ea.usefilter) {
var end = this.separate_nextcmd();
}
Expand Down Expand Up @@ -1380,6 +1380,7 @@ VimLParser.prototype.parse_cmd_loadkeymap = function() {
}

VimLParser.prototype.parse_cmd_lua = function() {
var lines = [];
this.reader.skip_white();
if (this.reader.peekn(2) == "<<") {
this.reader.getn(2);
Expand Down Expand Up @@ -1496,6 +1497,7 @@ VimLParser.prototype.parse_cmd_function = function() {
else {
var named = {};
while (TRUE) {
var varnode = Node(NODE_IDENTIFIER);
var token = tokenizer.get();
if (token.type == TOKEN_IDENTIFIER) {
if (!isargname(token.value) || token.value == "firstline" || token.value == "lastline") {
Expand All @@ -1505,7 +1507,6 @@ VimLParser.prototype.parse_cmd_function = function() {
throw Err(viml_printf("E853: Duplicate argument name: %s", token.value), token.pos);
}
named[token.value] = 1;
var varnode = Node(NODE_IDENTIFIER);
varnode.pos = token.pos;
varnode.value = token.value;
viml_add(node.rlist, varnode);
Expand All @@ -1529,7 +1530,6 @@ VimLParser.prototype.parse_cmd_function = function() {
}
}
else if (token.type == TOKEN_DOTDOTDOT) {
var varnode = Node(NODE_IDENTIFIER);
varnode.pos = token.pos;
varnode.value = token.value;
viml_add(node.rlist, varnode);
Expand Down Expand Up @@ -2433,6 +2433,7 @@ ExprTokenizer.prototype.get2 = function() {
return this.token(TOKEN_REG, r.getn(2), pos);
}
else if (c == "&") {
var s = "";
if ((r.p(1) == "g" || r.p(1) == "l") && r.p(2) == ":") {
var s = r.getn(3) + r.read_word();
}
Expand Down Expand Up @@ -2966,6 +2967,7 @@ ExprParser.prototype.parse_expr8 = function() {
if (token.type != TOKEN_SQCLOSE) {
throw Err(viml_printf("unexpected token: %s", token.value), token.pos);
}
var left = node;
}
else {
var right = this.parse_expr1();
Expand All @@ -2983,6 +2985,7 @@ ExprParser.prototype.parse_expr8 = function() {
if (token.type != TOKEN_SQCLOSE) {
throw Err(viml_printf("unexpected token: %s", token.value), token.pos);
}
var left = node;
}
else {
var node = Node(NODE_SUBSCRIPT);
Expand All @@ -2993,9 +2996,9 @@ ExprParser.prototype.parse_expr8 = function() {
if (token.type != TOKEN_SQCLOSE) {
throw Err(viml_printf("unexpected token: %s", token.value), token.pos);
}
var left = node;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this your expected?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes? Maybe I just don't understand what you asked?

It's exactly translated as I expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, you don't expect var here, I guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because it's "var", not "let".

Ideally, we may want to drop "var" here but it's not this p-r scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. hopefully we will fix this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VIm script's "let" ≒ JavaScript "var"

Cool! Easy to translate! Happy variable scope life! 😇 😇 😇

}
}
var left = node;
delete node;
}
else if (token.type == TOKEN_POPEN) {
Expand Down Expand Up @@ -3066,6 +3069,7 @@ ExprParser.prototype.parse_expr8 = function() {
ExprParser.prototype.parse_expr9 = function() {
var pos = this.reader.tell();
var token = this.tokenizer.get();
var node = Node(-1);
if (token.type == TOKEN_NUMBER) {
var node = Node(NODE_NUMBER);
node.pos = token.pos;
Expand Down Expand Up @@ -3317,13 +3321,14 @@ ExprParser.prototype.parse_identifier = function() {
var node = Node(NODE_IDENTIFIER);
node.pos = npos;
node.value = curly_parts[0].value;
return node;
}
else {
var node = Node(NODE_CURLYNAME);
node.pos = npos;
node.value = curly_parts;
return node;
}
return node;
}

ExprParser.prototype.parse_curly_parts = function() {
Expand Down Expand Up @@ -3391,6 +3396,7 @@ LvalueParser.prototype.parse_lv8 = function() {
var token = this.tokenizer.get();
if (!iswhite(c) && token.type == TOKEN_SQOPEN) {
var npos = token.pos;
var node = Node(-1);
if (this.tokenizer.peek().type == TOKEN_COLON) {
this.tokenizer.get();
var node = Node(NODE_SLICE);
Expand Down Expand Up @@ -3462,6 +3468,7 @@ LvalueParser.prototype.parse_lv8 = function() {
LvalueParser.prototype.parse_lv9 = function() {
var pos = this.reader.tell();
var token = this.tokenizer.get();
var node = Node(-1);
if (token.type == TOKEN_COPEN) {
this.reader.seek_set(pos);
var node = this.parse_identifier();
Expand Down Expand Up @@ -4075,6 +4082,7 @@ Compiler.prototype.compile_excall = function(node) {
}

Compiler.prototype.compile_let = function(node) {
var left = "";
if (node.left !== NIL) {
var left = this.compile(node.left);
}
Expand Down Expand Up @@ -4147,6 +4155,7 @@ Compiler.prototype.compile_while = function(node) {
}

Compiler.prototype.compile_for = function(node) {
var left = "";
if (node.left !== NIL) {
var left = this.compile(node.left);
}
Expand Down
Loading