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) return self.evaluate_foreach(cur)
elif isinstance(cur, mparser.PlusAssignmentNode): elif isinstance(cur, mparser.PlusAssignmentNode):
return self.evaluate_plusassign(cur) return self.evaluate_plusassign(cur)
elif isinstance(cur, mparser.IndexNode):
return self.evaluate_indexing(cur)
elif self.is_elementary_type(cur): elif self.is_elementary_type(cur):
return cur return cur
else: else:
@ -1903,6 +1905,18 @@ class Interpreter():
new_value = old_variable + [addition] new_value = old_variable + [addition]
self.set_variable(varname, new_value) 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): def is_elementary_type(self, v):
if isinstance(v, (int, float, str, bool, list)): if isinstance(v, (int, float, str, bool, list)):
return True return True

@ -210,6 +210,13 @@ class CodeBlockNode:
self.colno = colno self.colno = colno
self.lines = [] 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: class MethodNode:
def __init__(self, lineno, colno, source_object, name, args): def __init__(self, lineno, colno, source_object, name, args):
self.lineno = lineno self.lineno = lineno
@ -429,8 +436,15 @@ class Parser:
raise ParseException('Function call must be applied to plain id', raise ParseException('Function call must be applied to plain id',
left.lineno, left.colno) left.lineno, left.colno)
left = FunctionNode(left.lineno, left.colno, left.value, args) left = FunctionNode(left.lineno, left.colno, left.value, args)
while self.accept('dot'): go_again = True
left = self.method_call(left) 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 return left
def e8(self): def e8(self):
@ -492,6 +506,11 @@ class Parser:
return self.method_call(method) return self.method_call(method)
return 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): def foreachblock(self):
t = self.current t = self.current
self.expect('id') self.expect('id')

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

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

Loading…
Cancel
Save