aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2015-08-19 23:34:49 +0300
committerJussi Pakkanen <jpakkane@gmail.com>2015-08-19 23:34:49 +0300
commitee4e785b1ee366249cc2e2217dc54dc7af38ee73 (patch)
tree8afd8519422ca097a321488651ae1e4b99b3c8a7
parent60ff47f7361f0a92f4e852e3caa2e1ff5fc31191 (diff)
downloadmeson-ee4e785b1ee366249cc2e2217dc54dc7af38ee73.zip
meson-ee4e785b1ee366249cc2e2217dc54dc7af38ee73.tar.gz
meson-ee4e785b1ee366249cc2e2217dc54dc7af38ee73.tar.bz2
Add support for subscripting array objects with [].
-rw-r--r--interpreter.py14
-rw-r--r--mparser.py23
-rw-r--r--test cases/common/91 plusassign/meson.build8
-rw-r--r--test cases/failing/10 out of bounds/meson.build2
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]