-
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.
- Improved/fixed samples. - Operations of mutable types can now be chained as they return a reference to themselves for addition, subtraction, multiplication and division operations. - Wrapped fields and properties now return the value after set operations. - `EzrObject`'s `NewNothingConstant` and `NewBooleanConstant` are now static. - Private static symbols accessing using the static keyword no longer raise errors.
- Loading branch information
Showing
16 changed files
with
242 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
@ A turing machine written in ezr² | ||
@ Based on https://python-course.eu/applications-python/turing-machine.php | ||
|
||
object tape do | ||
private static constant blank_symbol: ` ` | ||
|
||
private tape: [] | ||
constant function initialize with tape_string: "" do | ||
for each char in tape_string do | ||
tape + char | ||
end | ||
end | ||
|
||
constant function to_string do | ||
str: '' | ||
for each char in tape do | ||
str + char | ||
end | ||
|
||
return str | ||
end | ||
|
||
constant function is_less_than with other do | ||
if type_of(other) ! "ezrSquared.Integer" do | ||
throw_error(unexpected_type_error("Expected an integer but got object of type \"" + type_name_of(other) + "\"!")) | ||
end | ||
|
||
return if other >= 0 and other < this.tape.length() do (this.tape < other) else do (static blank_symbol) | ||
end | ||
end | ||
|
||
object turing_machine do | ||
|
||
private tape: nothing | ||
private final_states: () | ||
private transition_functions: {} | ||
|
||
private head_position: 0 | ||
private current_state: nothing | ||
|
||
constant function initialize with tape, initial_state, final_states: nothing, transition_functions: nothing do | ||
this.tape: global tape(tape) | ||
this.current_state: initial_state | ||
|
||
if transition_functions ! nothing do | ||
this.transition_functions: transition_functions | ||
end | ||
|
||
if final_states ! nothing do | ||
this.final_states: final_states | ||
end | ||
end | ||
|
||
function get_tape do tape.to_string() | ||
|
||
function do_step do | ||
char_under_head: tape < head_position | ||
x: (current_state, char_under_head) | ||
|
||
if x in transition_functions do | ||
y: transition_functions < x | ||
this.tape < this.head_position: y < 1 | ||
|
||
if y < 2 = `R` do | ||
this.head_position:+ 1 | ||
else if y < 2 = `L` do | ||
this.head_position:- 1 | ||
end | ||
|
||
this.current_state: y < 0 | ||
end | ||
end | ||
|
||
function final do current_state in final_states | ||
end | ||
|
||
initial_state: "init" | ||
accepting_states: ("final",) | ||
transition_functions: {("init",`0`):("init", `1`, `R`), | ||
("init",`1`):("init", `0`, `R`), | ||
("init",` `):("final",` `, `N`)} | ||
final_states: ("final",) | ||
|
||
machine: turing_machine("010011001 ", initial_state, final_states, transition_functions) | ||
|
||
show("Input on tape: ", machine.get_tape()) | ||
|
||
while not machine.final() do | ||
machine.do_step() | ||
end | ||
|
||
show("Tape after processing: ", machine.get_tape()) |
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 |
---|---|---|
@@ -1,135 +1,150 @@ | ||
@ A demo for the ezr² programming language | ||
@ https://github.com/Uralstech/ezrSquared | ||
|
||
static object POSITION do | ||
EMPTY: `-` | ||
X: `X` | ||
O: `O` | ||
ANY: `/` | ||
end | ||
|
||
@ Main game matrix | ||
game: [["-", "-", "-"], | ||
["-", "-", "-"], | ||
["-", "-", "-"]] | ||
game: ([POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY], | ||
[POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY], | ||
[POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY]) | ||
|
||
@ Winning cases | ||
wins: [[["n", "n", "n"], | ||
["-", "-", "-"], | ||
["-", "-", "-"]], | ||
wins: (((POSITION.ANY, POSITION.ANY, POSITION.ANY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY)), | ||
|
||
[["-", "-", "-"], | ||
["n", "n", "n"], | ||
["-", "-", "-"]], | ||
((POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.ANY, POSITION.ANY, POSITION.ANY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY)), | ||
|
||
[["-", "-", "-"], | ||
["-", "-", "-"], | ||
["n", "n", "n"]], | ||
((POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.ANY, POSITION.ANY, POSITION.ANY)), | ||
|
||
[["n", "-", "-"], | ||
["n", "-", "-"], | ||
["n", "-", "-"]], | ||
((POSITION.ANY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.ANY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.ANY, POSITION.EMPTY, POSITION.EMPTY)), | ||
|
||
[["-", "n", "-"], | ||
["-", "n", "-"], | ||
["-", "n", "-"]], | ||
((POSITION.EMPTY, POSITION.ANY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.ANY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.ANY, POSITION.EMPTY)), | ||
|
||
[["-", "-", "n"], | ||
["-", "-", "n"], | ||
["-", "-", "n"]], | ||
((POSITION.EMPTY, POSITION.EMPTY, POSITION.ANY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.ANY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.ANY)), | ||
|
||
[["-", "-", "n"], | ||
["-", "n", "-"], | ||
["n", "-", "-"]], | ||
((POSITION.EMPTY, POSITION.EMPTY, POSITION.ANY), | ||
(POSITION.EMPTY, POSITION.ANY, POSITION.EMPTY), | ||
(POSITION.ANY, POSITION.EMPTY, POSITION.EMPTY)), | ||
|
||
[["n", "-", "-"], | ||
["-", "n", "-"], | ||
["-", "-", "n"]]] | ||
((POSITION.ANY, POSITION.EMPTY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.ANY, POSITION.EMPTY), | ||
(POSITION.EMPTY, POSITION.EMPTY, POSITION.ANY))) | ||
|
||
@ Function to show game matrix in more readable form | ||
function show_game do | ||
count to game.length as i do | ||
s: "" | ||
count to (game <= i).length as j do s: s + game <= i <= j + " " | ||
for each i in game do | ||
row: '' | ||
|
||
show(s) | ||
for each j in i do | ||
row + j | ||
row + ` ` | ||
end | ||
|
||
show(row) | ||
end | ||
end | ||
|
||
@ Function for checking if the game is over, and who has won | ||
function check_result do | ||
full: true | ||
count to wins.length as i do | ||
x_pts: 0 | ||
o_pts: 0 | ||
count to game.length as j do | ||
count to (game <= j).length as k do | ||
if wins <= i <= j <= k = "n" do | ||
if game <= j <= k = "x" do x_pts: x_pts + 1 | ||
if game <= j <= k = "o" do o_pts: o_pts + 1 | ||
|
||
for each case in wins do | ||
x_points: 0 | ||
o_points: 0 | ||
|
||
count to game.length() as i do | ||
count to (game < i).length() as j do | ||
if game < i < j = POSITION.EMPTY do | ||
full: false | ||
skip | ||
else if case < i < j = POSITION.EMPTY do | ||
skip | ||
end | ||
|
||
if game <= j <= k = "-" do full: false | ||
if game < i < j = POSITION.X do | ||
x_points:+ 1 | ||
else if game < i < j = POSITION.O do | ||
o_points:+ 1 | ||
end | ||
end | ||
|
||
if x_pts >= 3 do | ||
return "x" | ||
else if o_pts >= 3 do | ||
return "o" | ||
if x_points >= 3 do | ||
return POSITION.X | ||
else if o_points >= 3 do | ||
return POSITION.O | ||
end | ||
end | ||
end | ||
|
||
return full.as_integer().as_string() | ||
return if full do "FULL" else do false | ||
end | ||
|
||
@ Function to register the players" current move | ||
@ Function to register the player's current move | ||
function move_game with position, char do | ||
y: if position <= 3 do 0 else if position <= 6 do 1 else if position <= 9 do 2 else do -1 | ||
x: if position <= 3 do position - 1 else if position <= 6 do position - 4 else if position <= 9 do position - 7 else do -1 | ||
if (y < 0 or x < 0) or position <= 0 do return "INVALID POSITION" | ||
if game <= y <= x ! "-" do return "INVALID POSITION" | ||
if game < y < x ! POSITION.EMPTY do return "INVALID POSITION" | ||
|
||
gamey : game <= y | ||
|
||
gamey - x | ||
gamey.insert(x, char) | ||
|
||
game - y | ||
game.insert(y, gamey) | ||
|
||
return game | ||
(global game) < y < x: char | ||
end | ||
|
||
@ Showing the matrix | ||
show("\n") | ||
show_game() | ||
|
||
@ Main gameloop | ||
char: "x" | ||
char: POSITION.X | ||
while true do | ||
try do | ||
move: get(("Enter position (" + char) + ") ").as_integer() | ||
input: get("Enter position (" + char + ") ") | ||
|
||
if input.length() ! 1 do throw_error(unexpected_type_error("Expected single-digit number!")) | ||
|
||
move_char: "" + input < 0 | ||
if move_char < 48 or move_char > 57 do throw_error(unexpected_type_error("Expected single-digit number!")) | ||
|
||
move: move_char - 48 | ||
catch do | ||
show("INVALID INPUT") | ||
skip | ||
end | ||
|
||
move: move_game(move, char) | ||
if type_of(move) = "string" do | ||
if type_name_of(move) = "string" do | ||
show(move) | ||
skip | ||
else do | ||
game: move | ||
end | ||
|
||
show("\n") | ||
show_game() | ||
|
||
result: check_result() | ||
if result = "1" do | ||
if result = "FULL" do | ||
show("Draw!") | ||
stop | ||
else if result = "x" do | ||
else if result = POSITION.X do | ||
show("X has won!") | ||
stop | ||
else if result = "o" do | ||
else if result = POSITION.O do | ||
show("O has won!") | ||
stop | ||
end | ||
|
||
char: if char = "x" do "o" else do "x" | ||
char: if char = POSITION.X do POSITION.O else do POSITION.X | ||
end |
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
Oops, something went wrong.