Skip to content

Commit

Permalink
.NET 9 and some minor improvements
Browse files Browse the repository at this point in the history
- 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
Uralstech committed Nov 18, 2024
1 parent da46a74 commit a72a80e
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 130 deletions.
92 changes: 92 additions & 0 deletions samples/better_turing_machine.ezr2
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())
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,44 @@ function action with input_char, replace_with, move, new_state do
return false
end

accept: false
while old_tapehead ! tapehead do
old_tapehead: tapehead

show(tape, " with tapehead at index ", tapehead, " on state ", state)

if state = 0 do
if action(a, X, R, 1) do 1 else if action(B, B, R, 10) do 1 else if action(Z, Z, R, 7) do 1 else if action(b, U, R, 4) do 1
action(a, X, R, 1) or action(B, B, R, 10) or action(Z, Z, R, 7) or action(b, U, R, 4)
else if state = 1 do
if action(a, a, R, 1) do 1 else if action(b, b, R, 2) do 1 else if action(B, B, L, 11) do 1
action(a, a, R, 1) or action(b, b, R, 2) or action(B, B, L, 11)
else if state = 2 do
if action(b, b, R, 2) do 1 else if action(Z, Z, R, 2) do 1 else if action(a, Z, L, 3) do 1
action(b, b, R, 2) or action(Z, Z, R, 2) or action(a, Z, L, 3)
else if state = 3 do
if action(b, b, L, 3) do 1 else if action(Z, Z, L, 3) do 1 else if action(a, a, L, 3) do 1 else if action(X, X, R, 0) do 1
action(b, b, L, 3) or action(Z, Z, L, 3) or action(a, a, L, 3) or action(X, X, R, 0)
else if state = 4 do
if action(b, b, R, 4) do 1 else if action(Z, Z, R, 5) do 1 else if action(B, B, L, 15) do 1
action(b, b, R, 4) or action(Z, Z, R, 5) or action(B, B, L, 15)
else if state = 5 do
if action(Z, Z, R, 5) do 1 else if action(V, V, R, 5) do 1 else if action(b, V, L, 6) do 1
action(Z, Z, R, 5) or action(V, V, R, 5) or action(b, V, L, 6)
else if state = 6 do
if action(Z, Z, L, 6) do 1 else if action(V, V, L, 6) do 1 else if action(b, b, L, 6) do 1 else if action(U, U, R, 0) do 1
action(Z, Z, L, 6) or action(V, V, L, 6) or action(b, b, L, 6) or action(U, U, R, 0)
else if state = 7 do
if action(Z, Z, R, 7) do 1 else if action(V, V, R, 8) do 1
action(Z, Z, R, 7) or action(V, V, R, 8)
else if state = 8 do
if action(V, V, R, 8) do 1 else if action(B, B, R, 9) do 1
action(V, V, R, 8) or action(B, B, R, 9)
else if state = 11 do
if action(a, a, L, 11) do 1 else if action(X, X, R, 12) do 1
action(a, a, L, 11) or action(X, X, R, 12)
else if state = 12 do
if action(a, Z, R, 13) do 1
action(a, Z, R, 13)
else if state = 13 do
if action(a, X, R, 12) do 1 else if action(B, B, R, 14) do 1
action(a, X, R, 12) or action(B, B, R, 14)
else if state = 15 do
if action(b, b, L, 15) do 1 else if action(U, U, R, 16) do 1
action(b, b, L, 15) or action(U, U, R, 16)
else if state = 16 do
if action(b, V, R, 17) do 1
action(b, V, R, 17)
else if state = 17 do
if action(b, U, R, 16) do 1 else if action(B, B, R, 18) do 1
action(b, U, R, 16) or action(B, B, R, 18)
else do
item accept: true
accept: true
end
end

Expand Down
147 changes: 81 additions & 66 deletions samples/tic_tac_toe_demo.ezr2
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
6 changes: 3 additions & 3 deletions src/EzrSquared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<OutputTypes>Library</OutputTypes>
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<Platforms>AnyCPU</Platforms>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<LangVersion>12</LangVersion>
<TargetFrameworks>net9.0;netstandard2.1</TargetFrameworks>
<LangVersion>13</LangVersion>

<ImplicitUsings>Disable</ImplicitUsings>
<Nullable>Enable</Nullable>
Expand All @@ -28,7 +28,7 @@
<Title>ezr² Portable Library</Title>
<Description>The ezr² programming language, as a portable class library! This can be used to integrate ezr² as an embedded scripting language in your apps, websites and more!</Description>

<PackageVersion>0.11.2-unstable</PackageVersion>
<PackageVersion>0.11.3-unstable</PackageVersion>

<Authors>Udayshankar Ravikumar</Authors>
<Company>URAV ADVANCED LEARNING SYSTEMS PRIVATE LIMITED</Company>
Expand Down
Loading

0 comments on commit a72a80e

Please sign in to comment.