Added boolean data type.

pull/15/head
Jussi Pakkanen 12 years ago
parent faaa0df78b
commit bd6e542f1c
  1. 5
      interpreter.py
  2. 20
      nodes.py
  3. 19
      parser.py
  4. 3
      test cases/17 if/builder.txt

@ -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

@ -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.')

@ -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))

@ -0,0 +1,3 @@
project('if test', 'c')
var = true
Loading…
Cancel
Save