remove the pipe operator
This commit is contained in:
18
README.md
18
README.md
@@ -8,7 +8,7 @@ A very cool language
|
|||||||
* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/src/std)
|
* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/src/std)
|
||||||
* Produces tiny static executables (11KB for `hello.zr`)
|
* Produces tiny static executables (11KB for `hello.zr`)
|
||||||
* No libc required!
|
* No libc required!
|
||||||
* Has type inference, pipe operator, variadics, dynamic arrays, hashmaps, DNS resolver, etc.
|
* Has type inference, variadics, dynamic arrays, hashmaps, DNS resolver, etc.
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
```rust
|
```rust
|
||||||
@@ -17,7 +17,7 @@ func main[] : i64
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
io.println("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
guess := io.read_line() |> str.trim() |> str.parse_i64()
|
guess := str.parse_i64(io.read_line())
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.println("You win!")
|
io.println("You win!")
|
||||||
@@ -28,20 +28,6 @@ func main[] : i64
|
|||||||
io.println("Too high!")
|
io.println("Too high!")
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
|
||||||
func square[x: i64] : i64
|
|
||||||
return x * x
|
|
||||||
|
|
||||||
func sum[a: i64, b: i64] : i64
|
|
||||||
return a + b
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
|
||||||
|> alg.map(^square)
|
|
||||||
|> alg.reduce(^sum, 0)
|
|
||||||
|> io.println_i64()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
```
|
```
|
||||||
cargo install --git https://git.ton1.dev/toni/zern
|
cargo install --git https://git.ton1.dev/toni/zern
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
func square[x: i64] : i64
|
|
||||||
return x * x
|
|
||||||
|
|
||||||
func sum[a: i64, b: i64] : i64
|
|
||||||
return a + b
|
|
||||||
|
|
||||||
func main[] : i64
|
|
||||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
|
||||||
|> alg.map(^square)
|
|
||||||
|> alg.reduce(^sum, 0)
|
|
||||||
|> io.println_i64()
|
|
||||||
@@ -3,7 +3,7 @@ func main[] : i64
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
io.println("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
guess := io.read_line() |> str.trim() |> str.parse_i64()
|
guess := str.parse_i64(io.read_line())
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.println("You win!")
|
io.println("You win!")
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ func part1[lines: Array] : void
|
|||||||
for i in 0..lines->size
|
for i in 0..lines->size
|
||||||
line : str = array.nth(lines, i)
|
line : str = array.nth(lines, i)
|
||||||
dir := line[0]
|
dir := line[0]
|
||||||
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
n := str.parse_i64(str.substr(line, 1, str.len(line) - 1))
|
||||||
|
|
||||||
if dir == 'L'
|
if dir == 'L'
|
||||||
dial -= n
|
dial -= n
|
||||||
@@ -28,7 +28,7 @@ func part2[lines: Array] : void
|
|||||||
for i in 0..lines->size
|
for i in 0..lines->size
|
||||||
line : str = array.nth(lines, i)
|
line : str = array.nth(lines, i)
|
||||||
dir := line[0]
|
dir := line[0]
|
||||||
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
n := str.parse_i64(str.substr(line, 1, str.len(line) - 1))
|
||||||
|
|
||||||
if dir == 'L'
|
if dir == 'L'
|
||||||
for i in 0..n
|
for i in 0..n
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ func part1[ranges: Array] : void
|
|||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
for i in 0..ranges->size
|
for i in 0..ranges->size
|
||||||
parts := array.nth(ranges, i) |> str.split("-")
|
parts := str.split(array.nth(ranges, i), "-")
|
||||||
start := array.nth(parts, 0) |> str.parse_i64()
|
start := str.parse_i64(array.nth(parts, 0))
|
||||||
end := array.nth(parts, 1) |> str.parse_i64()
|
end := str.parse_i64(array.nth(parts, 1))
|
||||||
|
|
||||||
for n in (start)..end+1
|
for n in (start)..end+1
|
||||||
if part1_is_invalid_id(str.from_i64(n))
|
if part1_is_invalid_id(str.from_i64(n))
|
||||||
@@ -43,9 +43,9 @@ func part2[ranges: Array] : void
|
|||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
for i in 0..ranges->size
|
for i in 0..ranges->size
|
||||||
parts := array.nth(ranges, i) |> str.split("-")
|
parts := str.split(array.nth(ranges, i), "-")
|
||||||
start := array.nth(parts, 0) |> str.parse_i64()
|
start := str.parse_i64(array.nth(parts, 0))
|
||||||
end := array.nth(parts, 1) |> str.parse_i64()
|
end := str.parse_i64(array.nth(parts, 1))
|
||||||
|
|
||||||
for n in (start)..end+1
|
for n in (start)..end+1
|
||||||
if part2_is_invalid_id(str.from_i64(n))
|
if part2_is_invalid_id(str.from_i64(n))
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
// leaks a bit of memory but looks very cool
|
n := 37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|
||||||
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|
n_str := str.from_i64(n)
|
||||||
|> str.from_i64()
|
io.println(str.substr(n_str, 0, 10))
|
||||||
|> str.substr(0, 10)
|
|
||||||
|> io.println()
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func main[] : i64
|
|||||||
io.println("0. Quit")
|
io.println("0. Quit")
|
||||||
io.print("\n> ")
|
io.print("\n> ")
|
||||||
|
|
||||||
choice := io.read_line() |> str.parse_i64()
|
choice := str.parse_i64(io.read_line())
|
||||||
|
|
||||||
if choice == 0
|
if choice == 0
|
||||||
break
|
break
|
||||||
@@ -46,7 +46,7 @@ func main[] : i64
|
|||||||
io.println("============")
|
io.println("============")
|
||||||
else if choice == 2
|
else if choice == 2
|
||||||
io.print("\nEnter new task: ")
|
io.print("\nEnter new task: ")
|
||||||
task := io.read_line() |> str.trim()
|
task := str.trim(io.read_line())
|
||||||
|
|
||||||
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
||||||
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
||||||
@@ -56,7 +56,7 @@ func main[] : i64
|
|||||||
io.println("\nTask added\n")
|
io.println("\nTask added\n")
|
||||||
else if choice == 3
|
else if choice == 3
|
||||||
io.print("\nEnter task id: ")
|
io.print("\nEnter task id: ")
|
||||||
id := io.read_line() |> str.parse_i64()
|
id := str.parse_i64(io.read_line())
|
||||||
|
|
||||||
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
||||||
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
||||||
|
|||||||
@@ -439,37 +439,7 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expression(&mut self) -> Result<Expr, ZernError> {
|
fn expression(&mut self) -> Result<Expr, ZernError> {
|
||||||
self.pipe()
|
self.or_and()
|
||||||
}
|
|
||||||
|
|
||||||
fn pipe(&mut self) -> Result<Expr, ZernError> {
|
|
||||||
let mut expr = self.or_and()?;
|
|
||||||
|
|
||||||
while self.match_token(&[TokenType::Pipe]) {
|
|
||||||
let pipe = self.previous().clone();
|
|
||||||
let right = self.equality()?;
|
|
||||||
|
|
||||||
match right.kind {
|
|
||||||
ExprKind::Call {
|
|
||||||
callee,
|
|
||||||
paren,
|
|
||||||
args,
|
|
||||||
} => {
|
|
||||||
let mut new_args = args;
|
|
||||||
new_args.insert(0, expr);
|
|
||||||
expr = Expr::new(ExprKind::Call {
|
|
||||||
callee,
|
|
||||||
paren,
|
|
||||||
args: new_args,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return error!(pipe.loc, "tried to pipe into a non-call expression");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(expr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ pub enum TokenType {
|
|||||||
BitOr,
|
BitOr,
|
||||||
LogicalAnd,
|
LogicalAnd,
|
||||||
LogicalOr,
|
LogicalOr,
|
||||||
Pipe,
|
|
||||||
DoubleDot,
|
DoubleDot,
|
||||||
ShiftLeft,
|
ShiftLeft,
|
||||||
ShiftRight,
|
ShiftRight,
|
||||||
@@ -202,9 +201,7 @@ impl Tokenizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
'|' => {
|
'|' => {
|
||||||
if self.match_char('>') {
|
if self.match_char('|') {
|
||||||
self.add_token(TokenType::Pipe)?
|
|
||||||
} else if self.match_char('|') {
|
|
||||||
self.add_token(TokenType::LogicalOr)?
|
self.add_token(TokenType::LogicalOr)?
|
||||||
} else {
|
} else {
|
||||||
self.add_token(TokenType::BitOr)?
|
self.add_token(TokenType::BitOr)?
|
||||||
|
|||||||
Reference in New Issue
Block a user