Skip to content

Commit

Permalink
temporarily remove automatic rename as it's slow and buggy and I'm st…
Browse files Browse the repository at this point in the history
…ruggling to make it good
  • Loading branch information
bcpeinhardt committed Dec 22, 2024
1 parent c123708 commit 0eca869
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 186 deletions.
126 changes: 17 additions & 109 deletions priv/static/squared_away.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5155,72 +5155,6 @@ function visit_cross_labels(te, f) {
return new Ok(te);
}
}
function update_labels(ex, old, new$5) {
if (ex instanceof BinaryOp2) {
let t2 = ex.type_;
let lhs = ex.lhs;
let op = ex.op;
let rhs = ex.rhs;
let $ = update_labels(lhs, old, new$5);
let lhs$1 = $[0];
let u1 = $[1];
let $1 = update_labels(rhs, old, new$5);
let rhs$1 = $1[0];
let u2 = $1[1];
return [new BinaryOp2(t2, lhs$1, op, rhs$1), u1 || u2];
} else if (ex instanceof CrossLabel2) {
let type_2 = ex.type_;
let key = ex.key;
let row_label = ex.row_label;
let col_label = ex.col_label;
let row_label$1 = (() => {
let $ = row_label === old;
if ($) {
return new$5;
} else {
return row_label;
}
})();
let col_label$1 = (() => {
let $ = col_label === old;
if ($) {
return new$5;
} else {
return col_label;
}
})();
return [
new CrossLabel2(type_2, key, row_label$1, col_label$1),
row_label$1 === old || col_label$1 === old
];
} else if (ex instanceof Group2) {
let t2 = ex.type_;
let inner = ex.expr;
let $ = update_labels(inner, old, new$5);
let new$1 = $[0];
let updated = $[1];
return [new Group2(t2, new$1), updated];
} else if (ex instanceof Label2 && ex.txt === old) {
let type_2 = ex.type_;
let key = ex.key;
let def_key = ex.def_key;
let txt = ex.txt;
return [new Label2(type_2, key, def_key, new$5), true];
} else if (ex instanceof Label2) {
let l = ex;
return [l, false];
} else if (ex instanceof UnaryOp2) {
let t2 = ex.type_;
let op = ex.op;
let inner = ex.expr;
let $ = update_labels(inner, old, new$5);
let new$1 = $[0];
let updated = $[1];
return [new UnaryOp2(t2, op, new$1), updated];
} else {
return [ex, false];
}
}
function do_to_string2(te) {
if (te instanceof BooleanLiteral2) {
let b = te.b;
Expand Down Expand Up @@ -8443,6 +8377,7 @@ function dependency_list(loop$input, loop$te, loop$acc) {
}
}
function edit_cell(state, key, src) {
let old_cell = get4(state.cells, key);
let res = try$(
(() => {
let _pipe = scan(src);
Expand Down Expand Up @@ -8572,7 +8507,8 @@ function edit_cell(state, key, src) {
_pipe,
state$1,
(s, k) => {
return edit_cell(s, k, get4(s.cells, k).src);
let c = get4(s.cells, k);
return edit_cell(s, k, c.src);
}
);
})();
Expand Down Expand Up @@ -8811,42 +8747,14 @@ function update(model, msg) {
if (msg instanceof UserSetCellValue) {
let key = msg.key;
let val = msg.val;
let old = get_cell(model.compiler_state, key);
let model$1 = fold2(
(() => {
let _pipe = model.compiler_state.cells;
return to_list3(_pipe);
})(),
model,
(acc, g) => {
let k = g[0];
let c = g[1];
let $ = c.outcome;
if (!$.isOk()) {
return acc;
} else {
let cs = $[0];
let $1 = update_labels(cs.typechecked, old.src, val);
let new_te = $1[0];
let was_updated = $1[1];
if (!was_updated) {
return acc;
} else {
return acc.withFields({
compiler_state: edit_cell(
model.compiler_state,
k,
to_string11(new_te)
)
});
}
}
}
);
let model$2 = model$1.withFields({
compiler_state: edit_cell(model$1.compiler_state, key, val)
});
return [model$2, none()];
let compiler_state = (() => {
let _pipe = model.compiler_state;
return edit_cell(_pipe, key, val);
})();
return [
model.withFields({ compiler_state }),
none()
];
} else if (msg instanceof UserToggledFormulaMode) {
let display_formulas = msg.to;
return [
Expand Down Expand Up @@ -8917,7 +8825,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
267,
224,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
Expand All @@ -8937,7 +8845,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
277,
234,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $1 }
Expand Down Expand Up @@ -8979,7 +8887,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
300,
257,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $2 }
Expand Down Expand Up @@ -9042,7 +8950,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
349,
306,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
Expand All @@ -9062,7 +8970,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
356,
313,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $1 }
Expand Down Expand Up @@ -9104,7 +9012,7 @@ function update(model, msg) {
throw makeError(
"let_assert",
"squared_away",
378,
335,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $2 }
Expand Down
47 changes: 2 additions & 45 deletions src/squared_away.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -162,51 +162,8 @@ fn key_press_event(event, cell) {
fn update(model: Model, msg: Msg) -> #(Model, effect.Effect(Msg)) {
case msg {
UserSetCellValue(key, val) -> {
let old = compiler.get_cell(model.compiler_state, key)

// If updating a label,
// for every typed_expr that references the label,
// we need to update the source.
// We do this by passing in the new label name to the typechecked expression
// and re-serializing it to a string.
let model =
list.fold(model.compiler_state.cells |> grid.to_list, model, fn(acc, g) {
let #(k, c) = g

case c.outcome {
// If it never compiled in the first place, don't try to change it.
Error(_) -> acc
Ok(cs) -> {
// Create a new src string for the cell with the label updated
let #(new_te, was_updated) =
typed_expr.update_labels(cs.typechecked, old.src, val)

// If the src for the cell changed, we need to update it
case was_updated {
False -> acc
True ->
Model(
..acc,
compiler_state: compiler.edit_cell(
model.compiler_state,
k,
typed_expr.to_string(new_te),
),
)
}
}
}
})

// Set the actual value in the cell.
// The compiler will handle updating the cells dependencies.
let model =
Model(
..model,
compiler_state: compiler.edit_cell(model.compiler_state, key, val),
)

#(model, effect.none())
let compiler_state = model.compiler_state |> compiler.edit_cell(key, val)
#(Model(..model, compiler_state:), effect.none())
}
UserToggledFormulaMode(display_formulas) -> {
#(Model(..model, display_formulas:), effect.none())
Expand Down
7 changes: 6 additions & 1 deletion src/squared_away/compiler.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub fn get_interpreted(state: State) {
}

pub fn edit_cell(state: State, key: grid.GridKey, src: String) -> State {
let old_cell = grid.get(state.cells, key)

// Scan, parse, typecheck, and evaluate the cell
let res = {
use scanned <- result.try(
Expand Down Expand Up @@ -162,7 +164,10 @@ pub fn edit_cell(state: State, key: grid.GridKey, src: String) -> State {
let deps = state.deps_graph |> dict.get(key) |> result.unwrap(or: [])
let new_state =
deps
|> list.fold(state, fn(s, k) { edit_cell(s, k, grid.get(s.cells, k).src) })
|> list.fold(state, fn(s, k) {
let c = grid.get(s.cells, k)
edit_cell(s, k, c.src)
})
new_state
}

Expand Down
2 changes: 1 addition & 1 deletion src/squared_away/squared_away_lang/interpreter/value.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type Value {
Boolean(b: Bool)
TestFail
TestPass

// There are cells whose content is never meant to be used in a formula.
// Their "value" at runtime should be this DoNotEvaluate to ensure they
// are not used. This includes:
Expand Down
3 changes: 2 additions & 1 deletion src/squared_away/squared_away_lang/scanner.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ pub fn scan(src: String) -> Result(List(token.Token), scan_error.ScanError) {
"\"" <> rest ->
case get_str(rest, "") {
Error(e) -> Error(e)
Ok(#(str_content, "")) -> Ok([token.StringLiteral(str_content |> string.reverse)])
Ok(#(str_content, "")) ->
Ok([token.StringLiteral(str_content |> string.reverse)])
Ok(_) ->
Error(scan_error.ScanError("Found extra content after string literal"))
}
Expand Down
18 changes: 11 additions & 7 deletions src/squared_away/squared_away_lang/typechecker.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import gleam/bool
import gleam/int
import gleam/io
import gleam/list.{Continue, Stop}
import gleam/option.{None, Some}
import gleam/result
import gleam/io
import squared_away/squared_away_lang/error
import squared_away/squared_away_lang/grid
import squared_away/squared_away_lang/parser/expr
Expand Down Expand Up @@ -168,7 +168,6 @@ pub fn typecheck(
}
}
expr.LabelDef(txt) -> {

// Duplicate label definitions should be a type error
let defs =
grid.fold(env, 0, fn(count, _, expr) {
Expand Down Expand Up @@ -196,8 +195,9 @@ pub fn typecheck(
_ -> Continue(None)
}
})

let key = label_def_key

let key =
label_def_key
|> option.map(grid.cell_to_the_right(env, _))
|> option.map(option.from_result)
|> option.flatten
Expand All @@ -214,12 +214,15 @@ pub fn typecheck(
}
Ok(expr.LabelDef(ldtxt)) -> {
Error(
error.TypeError(type_error.TypeError("Label points to another label definition, cannot be used as a standalone label. Did you mean to use a cross_label?"))
error.TypeError(type_error.TypeError(
"Label points to another label definition, cannot be used as a standalone label. Did you mean to use a cross_label?",
)),
)
}
Ok(expr) -> {
case typecheck(env, expr) {
Ok(te) -> Ok(typed_expr.Label(type_: te.type_, key:, def_key:, txt:))
Ok(te) ->
Ok(typed_expr.Label(type_: te.type_, key:, def_key:, txt:))
Error(e) -> Error(e)
}
}
Expand Down Expand Up @@ -308,7 +311,8 @@ pub fn typecheck(
expr.UsdLiteral(cents) -> Ok(typed_expr.UsdLiteral(type_: typ.TUsd, cents:))
expr.PercentLiteral(p) -> Ok(typed_expr.PercentLiteral(typ.TPercent, p))
expr.IntegerLiteral(n) -> Ok(typed_expr.IntegerLiteral(type_: typ.TInt, n:))
expr.StringLiteral(txt) -> Ok(typed_expr.StringLiteral(type_: typ.TString, txt:))
expr.StringLiteral(txt) ->
Ok(typed_expr.StringLiteral(type_: typ.TString, txt:))
expr.Group(inner) -> {
use expr <- result.try(typecheck(env, inner))
Ok(typed_expr.Group(type_: expr.type_, expr:))
Expand Down
Loading

0 comments on commit 0eca869

Please sign in to comment.