meson/nodes.py

147 lines
4.1 KiB
Python
Raw Normal View History

2012-12-27 00:28:06 +08:00
#!/usr/bin/python3 -tt
# Copyright 2012 Jussi Pakkanen
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class Node():
2012-12-27 03:34:55 +08:00
def __init__(self, lineno):
self.line_number = lineno
def lineno(self):
return self.line_number
2012-12-27 00:28:06 +08:00
class Expression(Node):
pass
class Statement(Node):
pass
2013-01-26 02:40:00 +08:00
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
2012-12-27 00:28:06 +08:00
class AtomExpression(Expression):
2012-12-27 03:34:55 +08:00
def __init__(self, value, lineno):
Expression.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.value = value
2012-12-30 09:20:53 +08:00
def get_value(self):
return self.value
2012-12-27 00:28:06 +08:00
class StringExpression(Expression):
2012-12-27 03:34:55 +08:00
def __init__(self, value, lineno):
Expression.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.value = value
class AtomStatement(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, value, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
assert(type(value) == type(''))
self.value = value
2012-12-30 09:20:53 +08:00
def get_value(self):
return self.value
2012-12-27 00:28:06 +08:00
2013-01-26 02:40:00 +08:00
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
2013-01-26 03:06:08 +08:00
class IfStatement(Statement):
2013-01-26 03:25:52 +08:00
def __init__(self, clause, trueblock, falseblock, lineno):
2013-01-26 03:06:08 +08:00
Statement.__init__(self, lineno)
self.clause = clause
2013-01-26 03:25:52 +08:00
self.trueblock = trueblock
self.falseblock = falseblock
2013-01-26 03:06:08 +08:00
def get_clause(self):
return self.clause
2013-01-26 03:25:52 +08:00
def get_trueblock(self):
return self.trueblock
def get_falseblock(self):
return self.falseblock
2013-01-26 02:40:00 +08:00
2012-12-27 00:28:06 +08:00
class StringStatement(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, value, lineno):
2012-12-27 00:28:06 +08:00
assert(type(value) == type(''))
2012-12-27 03:34:55 +08:00
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.value = value
2012-12-27 05:37:41 +08:00
def get_string(self):
return self.value
2012-12-27 00:28:06 +08:00
class FunctionCall(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, func_name, arguments, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.func_name = func_name
self.arguments = arguments
def get_function_name(self):
return self.func_name.value
2012-12-27 00:28:06 +08:00
class MethodCall(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, object_name, method_name, arguments, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.object_name = object_name
self.method_name = method_name
self.arguments = arguments
class Assignment(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, var_name, value, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.var_name = var_name
self.value = value
class CodeBlock(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:28:06 +08:00
self.statements = []
2012-12-27 00:50:49 +08:00
def prepend(self, statement):
self.statements = [statement] + self.statements
def get_statements(self):
return self.statements
2012-12-27 00:50:49 +08:00
class Arguments(Statement):
2012-12-27 03:34:55 +08:00
def __init__(self, lineno):
Statement.__init__(self, lineno)
2012-12-27 00:50:49 +08:00
self.arguments = []
def prepend(self, statement):
self.arguments = [statement] + self.arguments
2012-12-27 00:28:06 +08:00
2012-12-27 05:37:41 +08:00
def __len__(self):
return len(self.arguments)
2012-12-27 00:28:06 +08:00
def statement_from_expression(expr):
2012-12-27 00:50:49 +08:00
if isinstance(expr, AtomExpression):
2012-12-27 03:34:55 +08:00
return AtomStatement(expr.value, expr.lineno())
2012-12-27 00:50:49 +08:00
if isinstance(expr, StringExpression):
2012-12-27 03:34:55 +08:00
return StringStatement(expr.value, expr.lineno())
2013-01-26 02:40:00 +08:00
if isinstance(expr, BoolExpression):
return BoolStatement(expr.value, expr.lineno())
2012-12-27 00:28:06 +08:00
raise RuntimeError('Can not convert unknown expression to a statement.')