From ee4e785b1ee366249cc2e2217dc54dc7af38ee73 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Wed, 19 Aug 2015 23:34:49 +0300 Subject: Add support for subscripting array objects with []. --- interpreter.py | 14 ++++++++++++++ mparser.py | 23 +++++++++++++++++++++-- test cases/common/91 plusassign/meson.build | 8 ++++---- test cases/failing/10 out of bounds/meson.build | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/interpreter.py b/interpreter.py index 91ec6aa..76964a6 100644 --- a/interpreter.py +++ b/interpreter.py @@ -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 diff --git a/mparser.py b/mparser.py index 83d9cca..455182a 100644 --- a/mparser.py +++ b/mparser.py @@ -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') diff --git a/test cases/common/91 plusassign/meson.build b/test cases/common/91 plusassign/meson.build index 0093d88..c1e47e0 100644 --- a/test cases/common/91 plusassign/meson.build +++ b/test cases/common/91 plusassign/meson.build @@ -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 diff --git a/test cases/failing/10 out of bounds/meson.build b/test cases/failing/10 out of bounds/meson.build index 5c7f267..f791675 100644 --- a/test cases/failing/10 out of bounds/meson.build +++ b/test cases/failing/10 out of bounds/meson.build @@ -1,4 +1,4 @@ project('out of bounds', 'c') x = [] -y = x.get(0) +y = x[0] -- cgit v1.1