Add support for subscripting array objects with [].

pull/233/head
Jussi Pakkanen 9 years ago
parent 60ff47f736
commit ee4e785b1e
  1. 14
      interpreter.py
  2. 23
      mparser.py
  3. 8
      test cases/common/91 plusassign/meson.build
  4. 2
      test cases/failing/10 out of bounds/meson.build

@ -1056,6 +1056,8 @@ class Interpreter():
return self.evaluate_foreach(cur)
elif isinstance(cur, mparser.PlusAssignmentNode):
return self.evaluate_plusassign(cur)
elif isinstance(cur, mparser.IndexNode):
return self.evaluate_indexing(cur)
elif self.is_elementary_type(cur):
return cur
else:
@ -1903,6 +1905,18 @@ class Interpreter():
new_value = old_variable + [addition]
self.set_variable(varname, new_value)
def evaluate_indexing(self, node):
assert(isinstance(node, mparser.IndexNode))
iobject = self.evaluate_statement(node.iobject)
if not isinstance(iobject, list):
raise InterpreterException('Tried to index a non-array object.')
index = self.evaluate_statement(node.index)
if not isinstance(index, int):
raise InterpreterException('Index value is not an integer.')
if index < -len(iobject) or index >= len(iobject):
raise InterpreterException('Index %d out of bounds of array of size %d.' % (index, len(iobject)))
return iobject[index]
def is_elementary_type(self, v):
if isinstance(v, (int, float, str, bool, list)):
return True

@ -210,6 +210,13 @@ class CodeBlockNode:
self.colno = colno
self.lines = []
class IndexNode:
def __init__(self, iobject, index):
self.iobject = iobject
self.index = index
self.lineno = iobject.lineno
self.colno = iobject.colno
class MethodNode:
def __init__(self, lineno, colno, source_object, name, args):
self.lineno = lineno
@ -429,8 +436,15 @@ class Parser:
raise ParseException('Function call must be applied to plain id',
left.lineno, left.colno)
left = FunctionNode(left.lineno, left.colno, left.value, args)
while self.accept('dot'):
left = self.method_call(left)
go_again = True
while go_again:
go_again = False
if self.accept('dot'):
go_again = True
left = self.method_call(left)
if self.accept('lbracket'):
go_again = True
left = self.index_call(left)
return left
def e8(self):
@ -492,6 +506,11 @@ class Parser:
return self.method_call(method)
return method
def index_call(self, source_object):
index_statement = self.statement()
self.expect('rbracket')
return IndexNode(source_object, index_statement)
def foreachblock(self):
t = self.current
self.expect('id')

@ -8,7 +8,7 @@ if x.length() != 1
error('Incorrect append')
endif
if x.get(0) != 'a'
if x[0] != 'a'
error('Incorrect append 2.')
endif
@ -20,7 +20,7 @@ if y.length() != 1
error('Immutability broken.')
endif
if y.get(0) != 'a'
if y[0] != 'a'
error('Immutability broken 2.')
endif
@ -28,11 +28,11 @@ if x.length() != 2
error('Incorrect append 3')
endif
if x.get(0) != 'a'
if x[0] != 'a'
error('Incorrect append 4.')
endif
if x.get(1) != 'b'
if x[1] != 'b'
error('Incorrect append 5.')
endif

@ -1,4 +1,4 @@
project('out of bounds', 'c')
x = []
y = x.get(0)
y = x[0]

Loading…
Cancel
Save