Skip to content

Commit

Permalink
Merge pull request #2 from Kjarrigan/add-first-real-code-sample
Browse files Browse the repository at this point in the history
Add the first real working script
  • Loading branch information
Flipez authored Sep 28, 2021
2 parents 5911e33 + 8b0d191 commit 5796d71
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
[![](http://github-actions.40ants.com/Flipez/rocket-lang/matrix.svg)](https://github.com/Flipez/rocket-lang)
[![Test](https://github.com/Flipez/rocket-lang/actions/workflows/go-test.yml/badge.svg)](https://github.com/Flipez/rocket-lang/actions/workflows/go-test.yml)
[![Test](https://github.com/Flipez/rocket-lang/actions/workflows/go-test.yml/badge.svg)](https://github.com/Flipez/rocket-lang/actions/workflows/go-test.yml)

# General

RocketLang as of version 0.9.5 is the _full_ (as in the book was worked through) version of [MonkeyLang](https://monkeylang.org/)

## How To?

* Download & Install a Version from the Release-Page
* `rocket-lang` without any arguments will start an interactive shell
* `rocket-lang FILE` will run the code in that file (no file extension check yet)
* Use _Javascript_ Highlighting in your editor for some convenience
* Checkout Code [Samples](examples/) for what is currently possible (and what not)
67 changes: 67 additions & 0 deletions examples/aoc2015_day1.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"// A solution for part 1 of https://adventofcode.com/2015/day/1"
"// Last Updated: 2021.09.28"
"// RocketLang Version: 0.9.6-rc1"
"------------------------------------"

let part_one = fn(input) {
let floor = 0
let idx = len(input)

"// There are no loops at the moment so use recursion instead"
calc(input, idx, floor)
}

let calc = fn(input,idx,floor) {
let char_to_value = {
"(": 1,
")": -1
}

if (idx == 0) {
return floor
}

let new_idx = idx - 1
let delta = char_to_value[input[new_idx]]

calc(input, new_idx, floor + delta)
}

"// Test some inputs to check that our code is correct..."
if (part_one(["(", "(", ")", ")"]) != 0) {
puts("Assertion ((( => 0 failed")
}

"// ... for multiple known results."
if (part_one([")","(",")",")","(",")",")"]) != -3) {
puts("Assertion )())()) => -3 failed")
}

"// There aren't any usefull string operations in RocketLang yet so you have to prepare"
"// the input code a bit - in this case as an array of strings, like this:"
let real_input = [
"(",")","(",")","(","(",")","(",")","(",")","(","(",")","(",")","(","(","(",")","(","(","(",")",")",
")","(","(","(",")","(","(","(","(","(",")","(",")","(","(","(","(","(",")",")",")","(",")","(","(",
"(","(","(",")",")","(","(","(","(","(","(","(","(",")","(","(","(","(","(","(","(","(","(","(",")",
"(","(","(","(",")",")","(","(",")","(",")","(","(",")","(","(","(",")","(",")","(","(",")","(","(",
")",")","(","(",")","(","(","(","(","(",")","(","(","(",")","(",")","(",")","(","(","(","(","(",")",
")","(","(","(","(","(","(","(",")","(","(",")","(","(","(","(",")","(",")",")","(","(",")","(","(",
"(","(","(",")",")",")","(",")",")","(","(",")",")","(","(",")","(","(",")","(",")",")",")",")",")",
")",")",")",")","(","(","(","(","(","(","(","(","(","(","(","(","(",")","(",")",")","(",")","(",")",
")","(",")",")","(","(",")",")",")",")","(","(","(","(",")","(",")","(",")","(","(","(","(","(",")",
"(","(","(","(",")","(","(",")","(","(",")","(",")","(","(",")","(","(",")","(",")","(","(",")","(",
"(","(","(","(","(","(","(",")",")","(","(",")",")","(","(",")",")","(",")",")",")",")","(","(","(",
")","(",")",")","(",")","(","(","(","(","(",")","(",")","(","(","(",")",")",")","(","(","(","(",")",
"(",")","(",")","(",")",")",")",")","(","(",")",")",")",")","(","(","(","(","(",")",")","(","(","(",
"(",")","(",")",")","(","(",")",")","(","(",")",")",")","(","(",")","(","(","(","(","(",")","(",")",
")",")",")","(",")",")","(","(","(",")","(","(",")",")","(","(","(","(",")","(","(","(","(","(",")",
"(","(","(",")","(","(",")",")","(",")",")",")",")","(","(","(",")",")",")","(",")","(",")","(","(",
")","(","(",")",")",")",")",")","(","(","(","(","(","(","(","(","(",")","(",")",")","(","(","(","(",
"(",")","(",")","(","(","(","(","(",")","(","(",")","(",")",")","(","(","(","(","(","(",")","(","(",
")","(",")",")","(",")",")",")","(",")",")","(","(","(","(",")",")",")","(",")","(","(",")","(","(",
")","(","(",")","(","(","(","(","(",")","(","(",")",")","(","(",")",")",")","(","(","(","(","(","(",
")","(",")","(","(",")","(",")","(",")","(","(",")","(","(","(","(",")",")","(",")",")","(","(","("
]

puts("Solution Day 1 Part 1: ")
puts(part_one(real_input))

0 comments on commit 5796d71

Please sign in to comment.