Added array support.

pull/15/head
Jussi Pakkanen 12 years ago
parent d5c50ce60a
commit 5269885f16
  1. 20
      interpreter.py
  2. 8
      nodes.py
  3. 6
      parser.py
  4. 6
      test cases/20 array/builder.txt
  5. 1
      test cases/20 array/func.c
  6. 3
      test cases/20 array/prog.c

@ -286,6 +286,8 @@ class Interpreter():
raise InvalidCode('Line %d: unknown variable "%s".' % (cur.lineno(), varname)) raise InvalidCode('Line %d: unknown variable "%s".' % (cur.lineno(), varname))
elif isinstance(cur, nodes.Comparison): elif isinstance(cur, nodes.Comparison):
return self.evaluate_comparison(cur) return self.evaluate_comparison(cur)
elif isinstance(cur, nodes.ArrayStatement):
return self.evaluate_arraystatement(cur)
else: else:
raise InvalidCode("Line %d: Unknown statement." % cur.lineno()) raise InvalidCode("Line %d: Unknown statement." % cur.lineno())
@ -394,7 +396,17 @@ class Interpreter():
c = ConfigureFile(self.subdir, args[0], args[1]) c = ConfigureFile(self.subdir, args[0], args[1])
self.build.configure_files.append(c) self.build.configure_files.append(c)
def flatten(self, args):
result = []
for a in args:
if isinstance(a, list):
result = result + self.flatten(a)
else:
result.append(a)
return result
def build_target(self, node, args, targetclass): def build_target(self, node, args, targetclass):
args = self.flatten(args)
for a in args: for a in args:
if not isinstance(a, str): if not isinstance(a, str):
raise InvalidArguments('Line %d: Argument %s is not a string.' % (node.lineno(), str(a))) raise InvalidArguments('Line %d: Argument %s is not a string.' % (node.lineno(), str(a)))
@ -421,7 +433,8 @@ class Interpreter():
if isinstance(value, InterpreterObject) or \ if isinstance(value, InterpreterObject) or \
isinstance(value, environment.PkgConfigDependency) or\ isinstance(value, environment.PkgConfigDependency) or\
isinstance(value, nodes.StringStatement) or\ isinstance(value, nodes.StringStatement) or\
isinstance(value, nodes.BoolStatement): isinstance(value, nodes.BoolStatement) or\
isinstance(value, list):
return True return True
return False return False
@ -500,6 +513,11 @@ class Interpreter():
return val1 != val2 return val1 != val2
else: else:
raise InvalidCode('You broke me.') raise InvalidCode('You broke me.')
def evaluate_arraystatement(self, cur):
arguments = self.reduce_arguments(cur.get_args())
return arguments
if __name__ == '__main__': if __name__ == '__main__':
code = """project('myawesomeproject') code = """project('myawesomeproject')
message('I can haz text printed out?') message('I can haz text printed out?')

@ -99,6 +99,14 @@ class Comparison(Statement):
def get_second(self): def get_second(self):
return self.second return self.second
class ArrayStatement(Statement):
def __init__(self, args, lineno):
Statement.__init__(self, lineno)
self.args = args
def get_args(self):
return self.args
class StringStatement(Statement): class StringStatement(Statement):
def __init__(self, value, lineno): def __init__(self, value, lineno):
assert(type(value) == type('')) assert(type(value) == type(''))

@ -119,11 +119,15 @@ def p_statement_assign(t):
'statement : expression ASSIGN statement' 'statement : expression ASSIGN statement'
t[0] = nodes.Assignment(t[1], t[3], t.lineno(1)) t[0] = nodes.Assignment(t[1], t[3], t.lineno(1))
def p_statement_equals(t): def p_statement_comparison(t):
'''statement : statement EQUALS statement '''statement : statement EQUALS statement
| statement NEQUALS statement''' | statement NEQUALS statement'''
t[0] = nodes.Comparison(t[1], t[2], t[3], t.lineno(1)) t[0] = nodes.Comparison(t[1], t[2], t[3], t.lineno(1))
def p_statement_array(t):
'''statement : LBRACKET args RBRACKET'''
t[0] = nodes.ArrayStatement(t[2], t.lineno(1))
def p_statement_func_call(t): def p_statement_func_call(t):
'statement : expression LPAREN args RPAREN' 'statement : expression LPAREN args RPAREN'
t[0] = nodes.FunctionCall(t[1], t[3], t.lineno(1)) t[0] = nodes.FunctionCall(t[1], t[3], t.lineno(1))

@ -0,0 +1,6 @@
project('array test', 'c')
arr = ['func.c', 'prog.c']
exe = executable('prog', arr)
add_test('arr test', exe)

@ -0,0 +1 @@
int func() { return 0; }

@ -0,0 +1,3 @@
extern int func();
int main(int argc, char **argv) { return func(); }
Loading…
Cancel
Save