aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xinterpreter.py5
-rw-r--r--nodes.py20
-rwxr-xr-xparser.py19
-rw-r--r--test cases/17 if/builder.txt3
4 files changed, 44 insertions, 3 deletions
diff --git a/interpreter.py b/interpreter.py
index c3d14b1..50c7a40 100755
--- a/interpreter.py
+++ b/interpreter.py
@@ -273,6 +273,8 @@ class Interpreter():
return self.method_call(cur)
elif isinstance(cur, nodes.StringStatement):
return cur
+ elif isinstance(cur, nodes.BoolStatement):
+ return cur
else:
raise InvalidCode("Unknown statement in line %d." % cur.lineno())
@@ -407,7 +409,8 @@ class Interpreter():
def is_assignable(self, value):
if isinstance(value, InterpreterObject) or \
isinstance(value, environment.PkgConfigDependency) or\
- isinstance(value, nodes.StringStatement):
+ isinstance(value, nodes.StringStatement) or\
+ isinstance(value, nodes.BoolStatement):
return True
return False
diff --git a/nodes.py b/nodes.py
index d58729a..4bbaa5d 100644
--- a/nodes.py
+++ b/nodes.py
@@ -27,6 +27,15 @@ class Expression(Node):
class Statement(Node):
pass
+class BoolExpression(Expression):
+ def __init__(self, value, lineno):
+ Expression.__init__(self, lineno)
+ self.value = value
+ assert(isinstance(value, bool))
+
+ def get_value(self):
+ return self.value
+
class AtomExpression(Expression):
def __init__(self, value, lineno):
Expression.__init__(self, lineno)
@@ -49,6 +58,15 @@ class AtomStatement(Statement):
def get_value(self):
return self.value
+class BoolStatement(Statement):
+ def __init__(self, value, lineno):
+ Statement.__init__(self, lineno)
+ assert(isinstance(value, bool))
+ self.value = value
+
+ def get_value(self):
+ return self.value
+
class StringStatement(Statement):
def __init__(self, value, lineno):
assert(type(value) == type(''))
@@ -107,4 +125,6 @@ def statement_from_expression(expr):
return AtomStatement(expr.value, expr.lineno())
if isinstance(expr, StringExpression):
return StringStatement(expr.value, expr.lineno())
+ if isinstance(expr, BoolExpression):
+ return BoolStatement(expr.value, expr.lineno())
raise RuntimeError('Can not convert unknown expression to a statement.')
diff --git a/parser.py b/parser.py
index 1d59800..a6af003 100755
--- a/parser.py
+++ b/parser.py
@@ -18,6 +18,9 @@ import ply.lex as lex
import ply.yacc as yacc
import nodes
+reserved = {'true' : 'TRUE',
+ 'false' : 'FALSE'}
+
tokens = ['LPAREN',
'RPAREN',
'LBRACKET',
@@ -32,7 +35,7 @@ tokens = ['LPAREN',
'STRING',
'EOL_CONTINUE',
'EOL',
- ]
+ ] + list(reserved.values())
t_EQUALS = '='
t_LPAREN = '\('
@@ -41,13 +44,17 @@ t_LBRACKET = '\['
t_RBRACKET = '\]'
t_LBRACE = '\{'
t_RBRACE = '\}'
-t_ATOM = '[a-zA-Z][_0-9a-zA-Z]*'
t_COMMENT = '\#[^\n]*'
t_COMMA = ','
t_DOT = '\.'
t_ignore = ' \t'
+def t_ATOM(t):
+ '[a-zA-Z][_0-9a-zA-Z]*'
+ t.type = reserved.get(t.value, 'ATOM')
+ return t
+
def t_STRING(t):
"'[^']*'"
t.value = t.value[1:-1]
@@ -88,6 +95,14 @@ def p_expression_atom(t):
'expression : ATOM'
t[0] = nodes.AtomExpression(t[1], t.lineno(1))
+def p_expression_bool(t):
+ '''expression : TRUE
+ | FALSE'''
+ if t[1] == 'true':
+ t[0] = nodes.BoolExpression(True, t.lineno(1))
+ else:
+ t[0] = nodes.BoolExpression(False, t.lineno(1))
+
def p_expression_string(t):
'expression : STRING'
t[0] = nodes.StringExpression(t[1], t.lineno(1))
diff --git a/test cases/17 if/builder.txt b/test cases/17 if/builder.txt
new file mode 100644
index 0000000..ca994d0
--- /dev/null
+++ b/test cases/17 if/builder.txt
@@ -0,0 +1,3 @@
+project('if test', 'c')
+
+var = true