Commit 82dd88f4 authored by Chris Jewell's avatar Chris Jewell
Browse files

Implemented array "[]" syntax in gemlang definition, and added an ArrayLiteral ASTNode.

parent f317808d
......@@ -27,7 +27,7 @@ from gem.gemlang.ast.ast_base import Statement, ASTNode, KwRef
from gem.gemlang.semantics.scope import Scope
__all__ = ['Expr', 'UnaryExpr', 'BinaryExpr', 'Call', 'KwArg',
'IdRef', 'Number', 'Integer', 'Float', 'String', 'TransitionDef', 'ArgList']
'IdRef', 'ArrayLiteral', 'Number', 'Integer', 'Float', 'String', 'TransitionDef', 'ArgList']
_BINARY_OPS = ['truediv', 'mul', 'matmul', 'add', 'sub', 'pow', 'lt', 'gt',
'le', 'ge', 'eq', 'ne']
......@@ -169,20 +169,31 @@ class IdRef(Expr):
return str(self.value)
## Data classes
# Data literals
class ArrayLiteral(Expr):
"""Represents an n-dimensional array literal.
:param elements: a list of array elements
"""
def __init__(self, elements, *args, **kwargs):
super().__init__(*args, **kwargs)
self.value = elements
def __str__(self):
return str(self.value)
def __repr__(self):
return f"{self.__class__.__name__} '{str(self.value)}'"
class Number(Expr):
"""Represents a number
:param data: a number
"""
def __init__(self, data, *args, **kwargs):
super().__init__(*args, **kwargs)
self.value = data # :ivar the value of the number.
if np.isscalar(data):
self.ndim = 0
else:
self.ndim = data.ndim
def __str__(self):
return str(self.value)
......
......@@ -54,6 +54,7 @@ _atom_expr: neg | not_ | _atom
neg: "-" _atom
not_: "!" _atom
_atom: "(" expr ")"
| array
| call
| idref
| number
......@@ -63,6 +64,7 @@ _atom: "(" expr ")"
call: idref "(" arglist ")"
//indexedvar: _atom "[" _subscriptlist "]"
array: "[" expr ("," expr)* "]"
_subscriptlist: expr ("," expr)* [","]
arglist: [(kwarg | expr) ("," (kwarg | expr))*]
kwarg: CNAME "=" expr
......
......@@ -139,6 +139,9 @@ class GEMParser(Transformer):
def idref(self, args, meta):
return IdRef(str(args[0]), meta=meta)
def array(self, args, meta):
return ArrayLiteral(args, meta=meta)
def number(self, args, meta):
if args[0].type == 'INT':
return Integer(args[0], meta=meta)
......
# Copyright 2019 Chris Jewell <c.jewell@lancaster.ac.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Copyright 2019 Chris Jewell <c.jewell@lancaster.ac.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
"""gemlib maths functions, such as operators, exponents, and square roots."""
from tensorflow import math as tfm
class Math:
def __init__(self, lib):
self.lib = lib
def add(self, a, b):
return self.lib.add(a, b)
def mul(self, a, b):
return self.lib.mul(a, b)
def sub(self, a, b):
return self.lib.sub(a, b)
def truediv(self, a, b):
return self.lib.truediv(a, b)
......@@ -104,6 +104,18 @@ class TestDeclParse(unittest.TestCase):
self.parser = Lark(grammar, start='gem', propagate_positions=True)
def test_1dArray(self):
stmt = """[3, 3, 3]"""
tree = self.parser.parse(stmt)
print(tree)
def test_2dArray(self):
stmt = """[[1,2],[3,4],[5,6]]"""
tree = self.parser.parse(stmt)
sp = GEMParser()
ast = sp.transform(tree)
print(serialize(ast))
def test_varassign(self):
stmt = """x = 1 + 3"""
tree = self.parser.parse(stmt)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment