-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mdbrnowski/interpretation
Complete interpretation
- Loading branch information
Showing
11 changed files
with
295 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
for i = 1:4 { | ||
if (i == 3) | ||
break; | ||
print i; | ||
} | ||
|
||
for i = 1:4 { | ||
if (i == 3) | ||
continue; | ||
print i; | ||
} | ||
|
||
i = 0; | ||
while (i < 4) { | ||
i += 1; | ||
if (i == 3) | ||
break; | ||
print i; | ||
} | ||
|
||
i = 0; | ||
while (i < 4) { | ||
i += 1; | ||
if (i == 3) | ||
continue; | ||
print i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
A = eye(2); | ||
print A[0]; | ||
print A[0, 1]; | ||
A[0, 1] = 2; | ||
print A; | ||
A[0] = [0, 2]; | ||
print A; | ||
A[0, 1] -= 2; | ||
print A; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
n = 4; | ||
for i = 1:n { | ||
print i; | ||
i = 10; | ||
print i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
A = ones(2, 2); | ||
B = ones(2, 2); | ||
A = A .+ B; | ||
print A; | ||
A = A .* A; | ||
print A; | ||
A = A .- B; | ||
print A; | ||
A = A ./ B; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
a = 2; | ||
print a; | ||
|
||
a -= 1; | ||
print a; | ||
|
||
a *= 3; | ||
print a; | ||
|
||
if (a == 3) { | ||
b = "OK"; | ||
print b; | ||
} | ||
|
||
b = 2 * a; | ||
print b; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
b = -4; | ||
a = 4; | ||
while (a >= b) { | ||
print a; | ||
a -= 1; | ||
b += 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from .values import Value | ||
|
||
|
||
class Memory: | ||
def __init__(self): | ||
self.variables: dict[str, Value] = {} | ||
|
||
def has_variable(self, name: str) -> bool: | ||
return name in self.variables | ||
|
||
def get(self, name: str) -> Value: | ||
return self.variables[name] | ||
|
||
def put(self, name: str, value: Value): | ||
self.variables[name] = value | ||
|
||
|
||
class MemoryStack: | ||
def __init__(self): | ||
self.stack: list[Memory] = [] | ||
|
||
def get(self, name: str) -> Value: | ||
for memory in self.stack: | ||
if memory.has_variable(name): | ||
return memory.get(name) | ||
|
||
def put(self, name: str, value: Value): | ||
for memory in self.stack: | ||
if memory.has_variable(name): | ||
memory.put(name, value) | ||
return | ||
self.stack[-1].put(name, value) | ||
|
||
def push_memory(self): | ||
self.stack.append(Memory()) | ||
|
||
def pop_memory(self): | ||
self.stack.pop() |
Oops, something went wrong.