blueprint-compiler/blueprintcompiler/language/expression.py
James Westman ae40f8416f
Tweak the way scopes work
Should make it clearer what a "variable" is and allow more flexibility
in what a variable can be (previously it could only be an object, but
now it can be e.g. a reference to the template or a special shortcut)
2022-07-23 15:10:28 -05:00

71 lines
2 KiB
Python

# expressions.py
#
# Copyright 2022 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .common import *
expr = Pratt()
class Expr(AstNode):
grammar = expr
def emit_xml(self, xml: XmlEmitter):
self.children[-1].emit_xml(xml)
class InfixExpr(AstNode):
@property
def lhs(self):
children = list(self.parent_by_type(Expr).children)
return children[children.index(self) - 1]
class IdentExpr(AstNode):
grammar = UseIdent("ident")
@property
def is_this(self):
return self.parent_by_type(Scope).this_name == self.tokens["ident"]
def emit_xml(self, xml: XmlEmitter):
if self.is_this:
raise CompilerBugError()
self.parent_by_type(Scope).variables[self.tokens["ident"]].emit_xml(xml)
class LookupOp(InfixExpr):
grammar = [".", UseIdent("property")]
def emit_xml(self, xml: XmlEmitter):
if isinstance(self.lhs, IdentExpr) and self.lhs.is_this:
xml.put_self_closing("lookup", name=self.tokens["property"], type=self.parent_by_type(Scope).this_type)
else:
xml.start_tag("lookup", name=self.tokens["property"])
self.lhs.emit_xml(xml)
xml.end_tag()
expr.children = [
Prefix(IdentExpr),
Prefix(["(", Expr, ")"]),
Infix(10, LookupOp),
]