Skip to content
Open
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
25 changes: 13 additions & 12 deletions plugin/vimcalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def getConsumed(self):
#recursive descent parser -- simple and befitting the needs of this small program
#generates the parse tree with evaluated decoration
def parse(expr):
tokens = tokenize(expr)
tokens = list(tokenize(expr))
if symbolCheck('ERROR', 0, tokens):
return 'Syntax error: ' + tokens[0].attrib
try:
Expand All @@ -267,7 +267,7 @@ def parse(expr):
return lineNode.assignedSymbol + ' = ' + process(lineNode.result)
else:
return 'Parse error: the expression is invalid.'
except ParseException, pe:
except ParseException as pe:
return 'Parse error: ' + pe.message

#this function returns an output string based on the global repl directives
Expand Down Expand Up @@ -411,13 +411,13 @@ def func(tokens):
try:
result = apply(lookupFunc(sym), argsNode.result)
return ParseNode(True, result, argsNode.consumeCount+3)
except TypeError, e:
raise ParseException, (str(e), argsNode.consumeCount+3)
except ValueError, e:
raise ParseException, (str(e), argsNode.consumeCount+3)
except TypeError as e:
raise ParseException(str(e), argsNode.consumeCount+3)
except ValueError as e:
raise ParseException(str(e), argsNode.consumeCount+3)
else:
error = 'missing matching parenthesis for function ' + sym + '.'
raise ParseException, (error, argsNode.consumeCount+2)
raise ParseException(error, argsNode.consumeCount+2)
else:
return ParseNode(False, 0, 0)

Expand Down Expand Up @@ -488,7 +488,7 @@ def expt(tokens):
return ParseNode(True, exprNode.result, exprNode.consumeCount+2)
else:
error = 'missing matching parenthesis in expression.'
raise ParseException, (error, exprNode.consumeCount+1)
raise ParseException(error, exprNode.consumeCount+1)
return ParseNode(False, 0, 0)

def number(tokens):
Expand Down Expand Up @@ -591,8 +591,9 @@ def foldr(fn, init, lst):
return fn(init, foldr(fn, lst[0], lst[1:]))

def symbolCheck(symbol, index, tokens):
if index < len(tokens):
if tokens[index].ID == symbol:
tokenList = list(tokens)
if index < len(tokenList):
if tokenList[index].ID == symbol:
return True
return False

Expand All @@ -614,7 +615,7 @@ def lookupSymbol(symbol):
return VCALC_SYMBOL_TABLE[symbol]
else:
error = "symbol '" + symbol + "' is not defined."
raise ParseException, (error, 0)
raise ParseException(error, 0)

def storeSymbol(symbol, value):
VCALC_SYMBOL_TABLE[symbol] = value
Expand Down Expand Up @@ -688,4 +689,4 @@ def lookupFunc(symbol):
return VCALC_FUNCTION_TABLE[symbol]
else:
error = "built-in function '" + symbol + "' does not exist."
raise ParseException, (error, 0)
raise ParseException(error, 0)
34 changes: 31 additions & 3 deletions plugin/vimcalc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

"TODO: move most of the functionality to autoload script if gets more complicated

if has('python3')
let scriptdirpy = expand("<sfile>:h") . '/'
exec "py3file " . scriptdirpy . "vimcalc.py"
endif

if has('python')
let scriptdirpy = expand("<sfile>:h") . '/'
exec "pyfile " . scriptdirpy . "vimcalc.py"
Expand Down Expand Up @@ -124,7 +129,7 @@ function! s:VCalc_DefineMappingsAndAutoCommands()
endfunction

function! s:VCalc_ValidateVim()
if has('python') != 1
if has('python3') != 1 && has('python') != 1
echohl WarningMsg | echomsg "VCalc requires the Python interface to be installed." | echohl None
return -1
endif
Expand All @@ -143,7 +148,11 @@ function! s:VCalc_REPL(continueInsert)

call <SID>VCalc_RecordHistory(expr)
"TODO: this breaks if a double quoted string is inputed.
exe "python repl(\"" . expr . "\")"
if has('python3')
exe "python3 repl(\"" . expr . "\")"
else
exe "python repl(\"" . expr . "\")"
endif

"if executed command don't continue -- may be a ':q'
if exists("w:vcalc_vim_command")
Expand Down Expand Up @@ -220,8 +229,27 @@ endfunction
" **** PYTHON **********************************************************************************************
" **********************************************************************************************************

if has('python')
if has('python3')
python3 << EOF

import vim

def repl(expr):
if expr != "":
result = parse(expr)
#if result is of the form: "!!!.*!!!" it is a vim command to execute.
m = re.match(r"^!!!(.*)!!!$", result)
if m:
vim.command("call append(line('$'), g:VCalc_Prompt)") #add prompt
vim.command(m.group(1))
vim.command("let w:vcalc_vim_command = 1")
else:
for str in result.split("\n"):
vim.command("call append(line('$'), \"" + str + "\")")
vim.command("if exists(\"w:vcalc_vim_command\") | unlet w:vcalc_vim_command | endif")
EOF

elseif has('python')
python << EOF

import vim
Expand Down