Skip to content

Commit

Permalink
Init arithmetic dims (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
pressureless committed Jun 8, 2021
1 parent 0ad16c1 commit a84a206
Show file tree
Hide file tree
Showing 9 changed files with 1,110 additions and 27 deletions.
3 changes: 2 additions & 1 deletion iheartla/la_grammar/LA_ebnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .base_ebnf import BASE
from .trigonometry_ebnf import TRIGONOMETRY
from .shared_ebnf import SHARED
from .arithmetic_ebnf import ARITHMETIC
START = r"""
@@grammar::LA
@@whitespace :: /(?!.*)/ #parse whitespace manually
Expand All @@ -15,7 +16,7 @@
{{separator_with_space} {hspace} vblock+:valid_block {separator_with_space}}+ {blank} $
;
"""
LA = START + KEYWORDS + NUMBER + OPERATORS + MATRIX + BASE + TRIGONOMETRY + SHARED
LA = START + KEYWORDS + NUMBER + OPERATORS + MATRIX + BASE + TRIGONOMETRY + SHARED + ARITHMETIC
#include :: "keywords.ebnf"
#include :: "number.ebnf"
#include :: "operators.ebnf"
Expand Down
48 changes: 48 additions & 0 deletions iheartla/la_grammar/arithmetic_ebnf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ARITHMETIC = r"""
arithmetic_expression::ArithExpression
=
| value:arithmetic_addition
| value:arithmetic_subtraction
| sign:['-'] value:arithmetic_term
;
arithmetic_addition::ArithAdd
=
left:arithmetic_expression {hspace} op:'+' {hspace} right:arithmetic_term
;
arithmetic_subtraction::ArithSubtract
=
left:arithmetic_expression {hspace} op:'-' {hspace} right:arithmetic_term
;
arithmetic_term
=
| arithmetic_multiplication
| arithmetic_division
| arithmetic_factor
;
arithmetic_multiplication::ArithMultiply
=
left:arithmetic_term {hspace} op:'⋅' {hspace} right:arithmetic_factor
| left:arithmetic_term {hspace} right:arithmetic_factor
;
arithmetic_division::ArithDivide
=
left:arithmetic_term {hspace} op:('/'|'÷') {hspace} right:arithmetic_factor
;
arithmetic_factor::ArithFactor
=
sub:arithmetic_subexpression
| id0:identifier
| num:number
;
arithmetic_subexpression::ArithSubexpression
=
'(' {hspace} value:arithmetic_expression {hspace} ')'
;
"""
2 changes: 1 addition & 1 deletion iheartla/la_grammar/shared_ebnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
dimension
=
integer | identifier
arithmetic_expression
;
la_type
Expand Down
3 changes: 2 additions & 1 deletion iheartla/la_grammar/simplified_grammar_ebnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from .trigonometry_ebnf import TRIGONOMETRY
from .shared_ebnf import SHARED
from .LA_ebnf import START
from .arithmetic_ebnf import ARITHMETIC

SIMPLIFIED = START + KEYWORDS + NUMBER + OPERATORS + MATRIX + BASE + TRIGONOMETRY + SHARED
SIMPLIFIED = START + KEYWORDS + NUMBER + OPERATORS + MATRIX + BASE + TRIGONOMETRY + SHARED + ARITHMETIC
#include :: "keywords.ebnf"
#include :: "number.ebnf"
#include :: "operators.ebnf"
Expand Down
248 changes: 242 additions & 6 deletions iheartla/la_local_parsers/default_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3747,13 +3747,9 @@ def block16():
)

@tatsumasu()
@nomemo
def _dimension_(self): # noqa
with self._choice():
with self._option():
self._integer_()
with self._option():
self._identifier_()
self._error('no available options')
self._arithmetic_expression_()

@tatsumasu()
def _la_type_(self): # noqa
Expand Down Expand Up @@ -4474,6 +4470,183 @@ def block4():
[]
)

@tatsumasu('ArithExpression')
@leftrec
def _arithmetic_expression_(self): # noqa
with self._choice():
with self._option():
self._arithmetic_addition_()
self.name_last_node('value')
with self._option():
self._arithmetic_subtraction_()
self.name_last_node('value')
with self._option():
with self._optional():
self._token('-')
self.name_last_node('sign')
self._arithmetic_term_()
self.name_last_node('value')
self._error('no available options')
self.ast._define(
['sign', 'value'],
[]
)

@tatsumasu('ArithAdd')
@nomemo
def _arithmetic_addition_(self): # noqa
self._arithmetic_expression_()
self.name_last_node('left')

def block1():
self._hspace_()
self._closure(block1)
self._token('+')
self.name_last_node('op')

def block3():
self._hspace_()
self._closure(block3)
self._arithmetic_term_()
self.name_last_node('right')
self.ast._define(
['left', 'op', 'right'],
[]
)

@tatsumasu('ArithSubtract')
@nomemo
def _arithmetic_subtraction_(self): # noqa
self._arithmetic_expression_()
self.name_last_node('left')

def block1():
self._hspace_()
self._closure(block1)
self._token('-')
self.name_last_node('op')

def block3():
self._hspace_()
self._closure(block3)
self._arithmetic_term_()
self.name_last_node('right')
self.ast._define(
['left', 'op', 'right'],
[]
)

@tatsumasu()
@leftrec
def _arithmetic_term_(self): # noqa
with self._choice():
with self._option():
self._arithmetic_multiplication_()
with self._option():
self._arithmetic_division_()
with self._option():
self._arithmetic_factor_()
self._error('no available options')

@tatsumasu('ArithMultiply')
@nomemo
def _arithmetic_multiplication_(self): # noqa
with self._choice():
with self._option():
self._arithmetic_term_()
self.name_last_node('left')

def block1():
self._hspace_()
self._closure(block1)
self._token('⋅')
self.name_last_node('op')

def block3():
self._hspace_()
self._closure(block3)
self._arithmetic_factor_()
self.name_last_node('right')
with self._option():
self._arithmetic_term_()
self.name_last_node('left')

def block6():
self._hspace_()
self._closure(block6)
self._arithmetic_factor_()
self.name_last_node('right')
self._error('no available options')
self.ast._define(
['left', 'op', 'right'],
[]
)

@tatsumasu('ArithDivide')
@nomemo
def _arithmetic_division_(self): # noqa
self._arithmetic_term_()
self.name_last_node('left')

def block1():
self._hspace_()
self._closure(block1)
with self._group():
with self._choice():
with self._option():
self._token('/')
with self._option():
self._token('÷')
self._error('no available options')
self.name_last_node('op')

def block4():
self._hspace_()
self._closure(block4)
self._arithmetic_factor_()
self.name_last_node('right')
self.ast._define(
['left', 'op', 'right'],
[]
)

@tatsumasu('ArithFactor')
def _arithmetic_factor_(self): # noqa
with self._choice():
with self._option():
self._arithmetic_subexpression_()
self.name_last_node('sub')
with self._option():
self._identifier_()
self.name_last_node('id0')
with self._option():
self._number_()
self.name_last_node('num')
self._error('no available options')
self.ast._define(
['id0', 'num', 'sub'],
[]
)

@tatsumasu('ArithSubexpression')
def _arithmetic_subexpression_(self): # noqa
self._token('(')

def block0():
self._hspace_()
self._closure(block0)
self._arithmetic_expression_()
self.name_last_node('value')

def block2():
self._hspace_()
self._closure(block2)
self._token(')')
self.ast._define(
['value'],
[]
)

@tatsumasu()
def _func_id_(self): # noqa
if len(self.new_func_list) > 0:
Expand Down Expand Up @@ -5140,6 +5313,30 @@ def less(self, ast): # noqa
def less_equal(self, ast): # noqa
return ast

def arithmetic_expression(self, ast): # noqa
return ast

def arithmetic_addition(self, ast): # noqa
return ast

def arithmetic_subtraction(self, ast): # noqa
return ast

def arithmetic_term(self, ast): # noqa
return ast

def arithmetic_multiplication(self, ast): # noqa
return ast

def arithmetic_division(self, ast): # noqa
return ast

def arithmetic_factor(self, ast): # noqa
return ast

def arithmetic_subexpression(self, ast): # noqa
return ast

def func_id(self, ast): # noqa
return ast

Expand Down Expand Up @@ -5702,6 +5899,45 @@ class LessEqualCondition(ModelBase):
right = None


class ArithExpression(ModelBase):
sign = None
value = None


class ArithAdd(ModelBase):
left = None
op = None
right = None


class ArithSubtract(ModelBase):
left = None
op = None
right = None


class ArithMultiply(ModelBase):
left = None
op = None
right = None


class ArithDivide(ModelBase):
left = None
op = None
right = None


class ArithFactor(ModelBase):
id0 = None
num = None
sub = None


class ArithSubexpression(ModelBase):
value = None


class IdentifierAlone(ModelBase):
id = None
value = None
Expand Down
Loading

0 comments on commit a84a206

Please sign in to comment.