Skip to content

Commit

Permalink
extract hella modules
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Aug 29, 2024
1 parent 1de9c91 commit a0eb676
Show file tree
Hide file tree
Showing 18 changed files with 1,788 additions and 1,595 deletions.
2,279 changes: 1,189 additions & 1,090 deletions priv/static/squared_away.mjs

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions src/squared_away.gleam
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import gleam/dict.{type Dict}
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import lustre
import lustre/attribute
import lustre/effect
import lustre/element
import lustre/element/html
import lustre/event
import squared_away/lang/interpreter
import squared_away/lang
import squared_away/lang/error
import squared_away/lang/interpreter/value

pub fn main() {
let app = lustre.application(init, update, view)
Expand All @@ -23,10 +24,7 @@ type Model {
Model(
active_cell: String,
src_grid: Dict(String, String),
value_grid: Dict(
String,
Result(interpreter.Value, interpreter.InterpretError),
),
value_grid: Dict(String, Result(value.Value, error.CompileError)),
)
}

Expand Down Expand Up @@ -58,10 +56,10 @@ type Msg {
}

fn update_grid(model: Model) -> Model {
let scanned = interpreter.scan_grid(model.src_grid)
let parsed = interpreter.parse_grid(scanned)
let typechecked = interpreter.typecheck_grid(parsed)
let value_grid = interpreter.interpret_grid(typechecked)
let scanned = lang.scan_grid(model.src_grid)
let parsed = lang.parse_grid(scanned)
let typechecked = lang.typecheck_grid(parsed)
let value_grid = lang.interpret_grid(typechecked)
Model(..model, value_grid:)
}

Expand Down Expand Up @@ -117,9 +115,10 @@ fn view(model: Model) -> element.Element(Msg) {
]),
])

let active_cell_value =
// We filled the grid ourself so this shouldn't ever panic, if it does it's a
// bug and I'd rather find it sooner.
let assert Ok(active_cell_value) =
dict.get(model.value_grid, model.active_cell)
|> result.unwrap(or: Ok(interpreter.Empty))

html.div([], [
html.div([], [grid]),
Expand Down
65 changes: 57 additions & 8 deletions src/squared_away/lang.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
//// The form formula language
//// term t :=
//// Integer 69
//// Float 420.69
//// Conditional if t then t else t
//// Bool TRUE | FALSE
//// Date September 10 1997
//// Addition +
import gleam/dict
import gleam/result
import squared_away/lang/error
import squared_away/lang/interpreter
import squared_away/lang/interpreter/value
import squared_away/lang/parser
import squared_away/lang/parser/expr
import squared_away/lang/scanner
import squared_away/lang/scanner/token
import squared_away/lang/typechecker
import squared_away/lang/typechecker/typed_expr

pub fn interpret_grid(
input: dict.Dict(String, Result(typed_expr.TypedExpr, error.CompileError)),
) -> dict.Dict(String, Result(value.Value, error.CompileError)) {
use acc, key, typed_expr <- dict.fold(input, dict.new())
case typed_expr {
Error(e) -> dict.insert(acc, key, Error(e))
Ok(typed_expr) -> {
let maybe_value = interpreter.interpret(input, typed_expr)
dict.insert(acc, key, maybe_value)
}
}
}

pub fn typecheck_grid(
input: dict.Dict(String, Result(expr.Expr, error.CompileError)),
) -> dict.Dict(String, Result(typed_expr.TypedExpr, error.CompileError)) {
use acc, key, expr <- dict.fold(input, dict.new())
case expr {
Error(e) -> dict.insert(acc, key, Error(e))
Ok(expr) -> {
let maybe_typed_expr = typechecker.typecheck(input, expr)
dict.insert(acc, key, maybe_typed_expr)
}
}
}

pub fn parse_grid(
input: dict.Dict(String, Result(List(token.Token), error.CompileError)),
) -> dict.Dict(String, Result(expr.Expr, error.CompileError)) {
use acc, key, toks <- dict.fold(input, dict.new())
case toks {
Error(e) -> dict.insert(acc, key, Error(e))
Ok(toks) -> {
let expr = parser.parse(toks)
dict.insert(acc, key, expr |> result.map_error(error.ParseError))
}
}
}

pub fn scan_grid(
input: dict.Dict(String, String),
) -> dict.Dict(String, Result(List(token.Token), error.CompileError)) {
use acc, key, src <- dict.fold(input, dict.new())
let maybe_scanned = scanner.scan(src) |> result.map_error(error.ScanError)
dict.insert(acc, key, maybe_scanned)
}
11 changes: 11 additions & 0 deletions src/squared_away/lang/error.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import squared_away/lang/interpreter/runtime_error
import squared_away/lang/parser/parse_error
import squared_away/lang/scanner/scan_error
import squared_away/lang/typechecker/type_error

pub type CompileError {
ScanError(scan_error.ScanError)
ParseError(parse_error.ParseError)
TypeError(type_error.TypeError)
RuntimeError(runtime_error.RuntimeError)
}
Loading

0 comments on commit a0eb676

Please sign in to comment.