Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
viddrobnic committed Jun 15, 2024
1 parent 240a24f commit 97da460
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 46 deletions.
92 changes: 49 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,57 @@ Advent of Code 2024.

For the language to be almost operational it should have:

- an interpreter,
- syntax highlighting,
- basic LSP.
- [x] an interpreter,
- [ ] syntax highlighting,
- [ ] basic LSP.

## Wishlist:
## Usage

1. Clone this repository:
```sh
git clone https://github.com/viddrobnic/aoc-lang.git
```
2. Build and install the interpreter:
```sh
cd aoc-lang
cargo install --path .
```
3. Run some code:
```sh
aoc-lang examples/hello_world.aoc
```

## Language features

AoC language supports the following features:

### Language features:

- [x] integers
- from float
- from string
- [x] floats
- from int
- from string
- [x] booleans
- from string
- [x] strings
- concatenate
- append
- split
- [x] arrays
- concatenate
- push
- pop
- unpacking via assignment: `[a, b] = [42, "foo"]`
- [x] hash maps
- add
- remove
- [x] arithmetic operations (`+`, `-`, `*`, `/`, `%`)
- [x] bit-wise operations (`&`, `|`, `!`)
- [x] comparison operations (`<`, `>`, `<=`, `>=`, `==`, `!=`)
- [x] logical operations (`!`, `&`, `|`)
- [x] variables
- [x] if/else statements
- [x] while loop
- [x] for loop
- [x] break
- [x] continue
- [x] functions
- return
- recursion
- [x] comments
- [x] stdin, stdout
- [x] imports
- [x] error reporting with line numbers
- integers
- floats
- booleans
- strings:
- arrays
- hash maps
- arithmetic operations (`+`, `-`, `*`, `/`, `%`)
- bit-wise operations (`&`, `|`, `!`)
- comparison operations (`<`, `>`, `<=`, `>=`, `==`, `!=`)
- logical operations (`!`, `&`, `|`)
- variables
- multi variable assignment (`[a, b] = [10, 20]`)
- if/else statements
- while loop
- for loop
- break
- continue
- functions
- comments
- stdin, stdout
- imports
- error reporting with line numbers

For more detailed overview of the syntax, see `examples` directory,
which contains examples of code with comments.

## Wishlist:

### Syntax highlighting

Expand Down
96 changes: 96 additions & 0 deletions examples/aoc_day_01.aoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Solution for day 01 of advent of code 2024
// https://adventofcode.com/2023/day/1
//
// Run it as:
// aoc-lang examples/aoc_day_01.aoc < input

// Read input
data = []
for (line = input(); line; line = input()) {
push(data, line)
}

// Part one
res = 0
for (i = 0; i < len(data); i = i + 1) {
chars = split(data[i], "")
n = 0

// First number
for (j = 0; j < len(chars); j = j + 1) {
if (int(chars[j])) {
n = int(chars[j])
break
}
}

// Last number
for (j = len(chars) - 1; j >= 0; j = j - 1) {
if (int(chars[j])) {
n = n * 10 + int(chars[j])
break
}
}

res = res + n
}

print("Part one: " + str(res))

// Part two
substr_is = fn(target_ch, position, lookup_ch) {
if (position + len(lookup_ch) > len(target_ch)) {
return false
}

for (i = 0; i < len(lookup_ch); i = i + 1) {
if (target_ch[position + i] != lookup_ch[i]) {
return false
}
}

return true
}

digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
digit = fn(chars, position) {
if (int(chars[position])) {
return int(chars[position])
}

for (d = 0; d < len(digits); d = d + 1) {
if (substr_is(chars, position, split(digits[d], ""))) {
return d + 1
}
}
}


res = 0
for (i = 0; i < len(data); i = i + 1) {
chars = split(data[i], "")
n = 0

// First number
for (j = 0; j < len(chars); j = j + 1) {
d = digit(chars, j)
if (d) {
n = d
break
}

}

// Last number
for (j = len(chars) - 1; j >= 0; j = j - 1) {
d = digit(chars, j)
if (d) {
n = n * 10 + d
break
}
}

res = res + n
}

print("Part two: " + str(res))
15 changes: 15 additions & 0 deletions examples/fibonacci.aoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Recursive fibonacci function implementation,
// which also uses a for loop to print the first
// 10 number in the sequence.

fib = fn(n) {
if (n <= 2) {
return 1
} else {
fib(n-1) + fib(n-2)
}
}

for (i = 1; i <= 10; i = i + 1) {
print(fib(i))
}
3 changes: 3 additions & 0 deletions examples/hello_world.aoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print("What is your name?")
name = input()
print("Hello " + name + "!")
Loading

0 comments on commit 97da460

Please sign in to comment.