Skip to content

Commit

Permalink
Make nd-arrays easier to create
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocfabini committed Jan 14, 2023
1 parent cfb88dc commit 1b3ee1f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/matrix_inversion.ans
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Before Invertion:
Antes de Inverter:
1.000000 2.000000 -1.000000
-2.000000 0.000000 1.000000
1.000000 -1.000000 0.000000
After Invertion:
Depois de Inverter:
1.000000 1.000000 2.000000
1.000000 1.000000 1.000000
2.000000 3.000000 4.000000
Expand Down
44 changes: 15 additions & 29 deletions examples/matrix_inversion.lat
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ munus mat_mul(a: &float, b: &float, c: &float, n: integer) {
}

munus mat_inverse(a: &float, Id: &float, n: integer) {
// Definir I como a matriz identidade
enim(i: integer = 0; i < n; i = i + 1) {
enim(j: integer = 0; j < n; j = j + 1) {
Id[i*n + j] = 0.0
}
Id[i*n + i] = 1.0
}

// Obter a matriz Inversa a menos de uns fatores
enim(i: integer = 0; i < n; i = i + 1) {
enim(j: integer = 0; j < n; j = j + 1) {
Expand Down Expand Up @@ -65,27 +57,21 @@ munus escrever_matrix(a: &float, n: integer) {
}

munus main() {
m_1: vec<float>[3][3]
m_2: vec<float>[3][3]
I: vec<float>[3][3]
m_1[0][0] = 1.0
m_1[0][1] = 2.0
m_1[0][2] = -1.0
m_1[1][0] = -2.0
m_1[1][1] = 0.0
m_1[1][2] = 1.0
m_1[2][0] = 1.0
m_1[2][1] = -1.0
m_1[2][2] = 0.0
m_2[0][0] = 1.0
m_2[0][1] = 2.0
m_2[0][2] = -1.0
m_2[1][0] = -2.0
m_2[1][1] = 0.0
m_2[1][2] = 1.0
m_2[2][0] = 1.0
m_2[2][1] = -1.0
m_2[2][2] = 0.0
m_1: vec<float>[3][3] = [
1.0, 2.0, -1.0,
-2.0, 0.0, 1.0,
1.0, -1.0, 0.0
]
m_2: vec<float>[3][3] = [
1.0, 2.0, -1.0,
-2.0, 0.0, 1.0,
1.0, -1.0, 0.0
]
I: vec<float>[3][3] = [
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
]

imprimo("Antes de Inverter:\n")
escrever_matrix(m_1, 3)
Expand Down
3 changes: 2 additions & 1 deletion lat/parsing/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ def p_pointer_init(p):

def p_array_literal_init(p):
"""
declaration_assignment : ID ':' Vtype ASSIGN '[' arrayitems ']'
declaration_assignment : ID ':' Vtype ndim ASSIGN '[' arrayitems ']'
| ID ':' Vtype ASSIGN '[' arrayitems ']'
"""
p[0] = parser.declaration_assignment_handler.handle(p, "array_literal_init")

Expand Down
32 changes: 22 additions & 10 deletions lat/semantics/_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,30 +307,42 @@ def __init__(self):
def handle(self, p, production) -> str:
return self.productions[production](p)

def _array_literal_init(self, p) -> str: # Declaring and initializing an array with a literal
def _array_literal_init(self, p) -> str: # Declaring and initializing an array with a literal
"""
declaration_assignment : ID ':' Vtype ASSIGN '[' arrayitems ']'
declaration_assignment : ID ':' Vtype ndim ASSIGN '[' arrayitems ']'
| ID ':' Vtype ASSIGN '[' arrayitems ']'
"""
if p[1] in p.parser.current_scope.Table: # If the variable already exists in the current scope table, report an error
if p[1] in p.parser.current_scope.Table: # If the variable already exists in the current scope table, report an error
compiler_error(p, 1, f"Variable {p[1]} is already defined")
compiler_note("Called from DeclarationAssignment._array_literal_init")
sys.exit(1)
array_shape = [p.parser.array_assign_items]
if len(p) == 9:
array_size = 1
array_shape = p.parser.arr_dim
for dim in p.parser.arr_dim:
array_size *= dim
if array_size != p.parser.array_assign_items:
compiler_error(p, 1, f"Initialization of array of type '{p[3]}' with {p.parser.array_assign_items} items. Expected {array_size}")
compiler_note("Called from DeclarationAssignment._array_literal_init")
sys.exit(1)
for i in range(p.parser.array_assign_items):
item = p.parser.type_checker.pop()
if item != p[3][4:-1]:
compiler_error(p, 5, f"Initialization of array of type '{p[3]}' with item of type '{item}'. Look at item {p.parser.array_assign_items-i}")
compiler_note("Called from DeclarationAssignment._array_literal_init")
sys.exit(1)

p[3] = p[3].replace(" ", "") # Remove the spaces from the type
if p.parser.current_scope.level == 0: # If the variable is declared in the global scope, add it to the global scope
p.parser.current_scope.add(p[1], p[3], (p.parser.global_count, p.parser.global_count + p.parser.array_assign_items - 1), array_shape=[p.parser.array_assign_items])
p[3] = p[3].replace(" ", "") # Remove the spaces from the type
if p.parser.current_scope.level == 0: # If the variable is declared in the global scope, add it to the global scope
p.parser.current_scope.add(p[1], p[3], (p.parser.global_count, p.parser.global_count+p.parser.array_assign_items-1), array_shape=copy(array_shape))
p.parser.global_count += p.parser.array_assign_items
else: # If the variable is declared in a function scope, add it to the function scope
p.parser.current_scope.add(p[1], p[3], (p.parser.frame_count, p.parser.frame_count + p.parser.array_assign_items - 1), array_shape=[p.parser.array_assign_items])
else: # If the variable is declared in a function scope, add it to the function scope
p.parser.current_scope.add(p[1], p[3], (p.parser.frame_count, p.parser.frame_count+p.parser.array_assign_items-1), array_shape=copy(array_shape))
p.parser.frame_count += p.parser.array_assign_items
p.parser.array_assign_items = 0 # Reset the array assign items counter
return p[6]
p.parser.arr_dim = [] # Reset the array dimension
p.parser.array_assign_items = 0 # Reset the array assign items counter
return p[len(p)-2]

def _array_range_init(self, p) -> str: # Declaring and initializing an array with a range TODO: turn integer into expression if possible
"""
Expand Down

0 comments on commit 1b3ee1f

Please sign in to comment.