Skip to content

Commit

Permalink
testing if it's this slow once deployed, also adds max
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Dec 23, 2024
1 parent 0eca869 commit 333f2c0
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 13 deletions.
118 changes: 105 additions & 13 deletions priv/static/squared_away.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,14 @@ function min(a, b) {
return b;
}
}
function max(a, b) {
let $ = a > b;
if ($) {
return a;
} else {
return b;
}
}
function ceiling2(x) {
return ceiling(x);
}
Expand Down Expand Up @@ -1502,7 +1510,7 @@ function min2(a, b) {
return b;
}
}
function max(a, b) {
function max2(a, b) {
let $ = a > b;
if ($) {
return a;
Expand Down Expand Up @@ -4790,6 +4798,19 @@ function min3(r1, r2) {
return r2;
}
}
function max3(r1, r2) {
let $ = subtract2(r1, r2);
let x = $.numerator;
let y = $.denominator;
let num_neg = isEqual(compare4(x, from2(0)), new Lt());
let den_neg = isEqual(compare4(y, from2(0)), new Lt());
let $1 = num_neg === den_neg;
if (!$1) {
return r2;
} else {
return r1;
}
}

// build/dev/javascript/squared_away/squared_away/squared_away_lang/parser/expr.mjs
var Empty2 = class extends CustomType {
Expand Down Expand Up @@ -4912,6 +4933,8 @@ var MustBe = class extends CustomType {
};
var Minimum = class extends CustomType {
};
var Maximum = class extends CustomType {
};
var Negate = class extends CustomType {
};
var Not = class extends CustomType {
Expand Down Expand Up @@ -4945,8 +4968,10 @@ function binary_to_string(b) {
return "-";
} else if (b instanceof MustBe) {
return "mustbe";
} else {
} else if (b instanceof Minimum) {
return "min";
} else {
return "max";
}
}
function unary_to_string(u) {
Expand Down Expand Up @@ -5295,8 +5320,10 @@ function describe_binary_op_kind_for_err(bo) {
return "Subtraction `-`";
} else if (bo instanceof MustBe) {
return "MustBe `mustbe`";
} else {
} else if (bo instanceof Minimum) {
return "Minimum `min`";
} else {
return "Maximum `max`";
}
}
function to_renderable_error(te) {
Expand Down Expand Up @@ -5624,6 +5651,10 @@ function interpret(loop$env, loop$expr) {
let a = lhs2.n;
let b = rhs2.n;
return new Ok(new Integer(min2(a, b)));
} else if (lhs2 instanceof Integer && op instanceof Maximum && rhs2 instanceof Integer) {
let a = lhs2.n;
let b = rhs2.n;
return new Ok(new Integer(max2(a, b)));
} else if (lhs2 instanceof FloatingPointNumber && op instanceof Add && rhs2 instanceof FloatingPointNumber) {
let a = lhs2.f;
let b = rhs2.f;
Expand Down Expand Up @@ -5668,6 +5699,10 @@ function interpret(loop$env, loop$expr) {
let a = lhs2.f;
let b = rhs2.f;
return new Ok(new FloatingPointNumber(min(a, b)));
} else if (lhs2 instanceof FloatingPointNumber && op instanceof Maximum && rhs2 instanceof FloatingPointNumber) {
let a = lhs2.f;
let b = rhs2.f;
return new Ok(new FloatingPointNumber(max(a, b)));
} else if (lhs2 instanceof FloatingPointNumber && op instanceof Power && rhs2 instanceof FloatingPointNumber) {
let base = lhs2.f;
let exponent = rhs2.f;
Expand Down Expand Up @@ -5697,7 +5732,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
161,
165,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
Expand Down Expand Up @@ -5733,7 +5768,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
183,
187,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
Expand Down Expand Up @@ -5787,6 +5822,10 @@ function interpret(loop$env, loop$expr) {
let d = lhs2.cents;
let p2 = rhs2.cents;
return new Ok(new Usd(min3(d, p2)));
} else if (lhs2 instanceof Usd && op instanceof Maximum && rhs2 instanceof Usd) {
let d = lhs2.cents;
let p2 = rhs2.cents;
return new Ok(new Usd(max3(d, p2)));
} else if (lhs2 instanceof Percent && op instanceof Multiply && rhs2 instanceof Usd) {
let p2 = lhs2.percent;
let d = rhs2.cents;
Expand All @@ -5811,6 +5850,10 @@ function interpret(loop$env, loop$expr) {
let p1 = lhs2.percent;
let p2 = rhs2.percent;
return new Ok(new Percent(min3(p1, p2)));
} else if (lhs2 instanceof Percent && op instanceof Maximum && rhs2 instanceof Percent) {
let p1 = lhs2.percent;
let p2 = rhs2.percent;
return new Ok(new Percent(max3(p1, p2)));
} else if (lhs2 instanceof Percent && op instanceof Power && rhs2 instanceof Percent) {
let base = lhs2.percent;
let exponent = rhs2.percent;
Expand Down Expand Up @@ -5891,7 +5934,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
303,
313,
"",
"Pattern match failed, no pattern matched the value.",
{ value: $ }
Expand Down Expand Up @@ -5982,7 +6025,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
355,
365,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -6003,7 +6046,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
363,
373,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -6024,7 +6067,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
371,
381,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand Down Expand Up @@ -6094,7 +6137,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
412,
422,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -6120,7 +6163,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
423,
433,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -6141,7 +6184,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
433,
443,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand Down Expand Up @@ -6260,6 +6303,8 @@ var MustBe2 = class extends CustomType {
};
var Minimum2 = class extends CustomType {
};
var Maximum2 = class extends CustomType {
};
var StringLiteral3 = class extends CustomType {
constructor(content_without_quotes) {
super();
Expand Down Expand Up @@ -6698,6 +6743,33 @@ function try_parse_binary_ops(tokens) {
);
}
);
} else if (tokens.atLeastLength(1) && tokens.head instanceof Maximum2) {
let rest = tokens.tail;
return try$(
do_parse2(rest),
(_use0) => {
let rhs = _use0[0];
let rest$1 = _use0[1];
return guard(
isEqual(rhs, new Empty2()),
new Error(
new ParseError(
"No item on right hand side of binary operation."
)
),
() => {
return new Ok(
[
(_capture) => {
return new BinaryOp(_capture, new Maximum(), rhs);
},
rest$1
]
);
}
);
}
);
} else {
return new Error(new ParseError("Not a binary operation"));
}
Expand Down Expand Up @@ -7225,6 +7297,10 @@ function do_scan(loop$src, loop$acc) {
let rest = src.slice(3);
loop$src = trim_left2(rest);
loop$acc = prepend(new Minimum2(), acc);
} else if (src.startsWith("max")) {
let rest = src.slice(3);
loop$src = trim_left2(rest);
loop$acc = prepend(new Maximum2(), acc);
} else if (src.startsWith("&&")) {
let rest = src.slice(2);
loop$src = trim_left2(rest);
Expand Down Expand Up @@ -8011,6 +8087,10 @@ function typecheck(env, expr) {
return new Ok(
new BinaryOp2(new TFloat(), lhs2, op, rhs2)
);
} else if ($ instanceof TFloat && op instanceof Maximum && $1 instanceof TFloat) {
return new Ok(
new BinaryOp2(new TFloat(), lhs2, op, rhs2)
);
} else if ($ instanceof TFloat && op instanceof Power && $1 instanceof TInt) {
return new Ok(
new BinaryOp2(new TFloat(), lhs2, op, rhs2)
Expand Down Expand Up @@ -8043,6 +8123,10 @@ function typecheck(env, expr) {
return new Ok(
new BinaryOp2(new TUsd(), lhs2, op, rhs2)
);
} else if ($ instanceof TUsd && op instanceof Maximum && $1 instanceof TUsd) {
return new Ok(
new BinaryOp2(new TUsd(), lhs2, op, rhs2)
);
} else if ($ instanceof TUsd && op instanceof Multiply && $1 instanceof TUsd) {
return new Error(
new TypeError2(
Expand Down Expand Up @@ -8077,6 +8161,10 @@ function typecheck(env, expr) {
return new Ok(
new BinaryOp2(new TPercent(), lhs2, op, rhs2)
);
} else if ($ instanceof TPercent && op instanceof Maximum && $1 instanceof TPercent) {
return new Ok(
new BinaryOp2(new TPercent(), lhs2, op, rhs2)
);
} else if ($ instanceof TBool && op instanceof And && $1 instanceof TBool) {
return new Ok(
new BinaryOp2(new TBool(), lhs2, op, rhs2)
Expand Down Expand Up @@ -8109,6 +8197,10 @@ function typecheck(env, expr) {
return new Ok(
new BinaryOp2(new TInt(), lhs2, op, rhs2)
);
} else if ($ instanceof TInt && op instanceof Maximum && $1 instanceof TInt) {
return new Ok(
new BinaryOp2(new TInt(), lhs2, op, rhs2)
);
} else if ($ instanceof TInt && op instanceof Multiply && $1 instanceof TUsd) {
return new Ok(
new BinaryOp2(new TUsd(), lhs2, op, rhs2)
Expand Down Expand Up @@ -9156,7 +9248,7 @@ function recalculate_col_width(model, col2) {
);
}
})();
let _pipe$1 = fold2(_pipe, min_cell_size_ch - 1, max);
let _pipe$1 = fold2(_pipe, min_cell_size_ch - 1, max2);
return add(_pipe$1, 1);
}
function view(model) {
Expand Down
10 changes: 10 additions & 0 deletions src/squared_away/squared_away_lang/interpreter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub fn interpret(
Ok(value.Boolean(a <= b))
value.Integer(a), expr.Minimum, value.Integer(b) ->
Ok(value.Integer(int.min(a, b)))
value.Integer(a), expr.Maximum, value.Integer(b) ->
Ok(value.Integer(int.max(a, b)))

// Float x Float
value.FloatingPointNumber(a), expr.Add, value.FloatingPointNumber(b) ->
Expand Down Expand Up @@ -132,6 +134,8 @@ pub fn interpret(
-> Ok(value.Boolean(a <=. b))
value.FloatingPointNumber(a), expr.Minimum, value.FloatingPointNumber(b)
-> Ok(value.FloatingPointNumber(float.min(a, b)))
value.FloatingPointNumber(a), expr.Maximum, value.FloatingPointNumber(b)
-> Ok(value.FloatingPointNumber(float.max(a, b)))

value.FloatingPointNumber(base),
expr.Power,
Expand Down Expand Up @@ -217,6 +221,9 @@ pub fn interpret(
value.Usd(d), expr.Minimum, value.Usd(p) -> {
Ok(value.Usd(rational.min(d, p)))
}
value.Usd(d), expr.Maximum, value.Usd(p) -> {
Ok(value.Usd(rational.max(d, p)))
}

// Percent x Usd
value.Percent(p), expr.Multiply, value.Usd(d) -> {
Expand All @@ -241,6 +248,9 @@ pub fn interpret(
value.Percent(p1), expr.Minimum, value.Percent(p2) -> {
Ok(value.Percent(rational.min(p1, p2)))
}
value.Percent(p1), expr.Maximum, value.Percent(p2) -> {
Ok(value.Percent(rational.max(p1, p2)))
}
value.Percent(base), expr.Power, value.Percent(exponent) -> {
let fractional_exponent = !rational.is_whole_number(exponent)
use <- bool.guard(
Expand Down
10 changes: 10 additions & 0 deletions src/squared_away/squared_away_lang/parser.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ fn try_parse_binary_ops(
)
Ok(#(expr.BinaryOp(_, expr.Minimum, rhs), rest))
}
[token.Maximum, ..rest] -> {
use #(rhs, rest) <- result.try(do_parse(rest))
use <- bool.guard(
rhs == expr.Empty,
Error(parse_error.ParseError(
"No item on right hand side of binary operation.",
)),
)
Ok(#(expr.BinaryOp(_, expr.Maximum, rhs), rest))
}
_ -> Error(parse_error.ParseError("Not a binary operation"))
}
}
2 changes: 2 additions & 0 deletions src/squared_away/squared_away_lang/parser/expr.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub type BinaryOpKind {
Or
MustBe
Minimum
Maximum
}

pub fn binary_to_string(b: BinaryOpKind) -> String {
Expand All @@ -55,6 +56,7 @@ pub fn binary_to_string(b: BinaryOpKind) -> String {
Subtract -> "-"
MustBe -> "mustbe"
Minimum -> "min"
Maximum -> "max"
}
}

Expand Down
1 change: 1 addition & 0 deletions src/squared_away/squared_away_lang/scanner.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ fn do_scan(
"avg" <> rest ->
do_scan(string.trim_left(rest), [token.BuiltinAvg(option.None), ..acc])
"min" <> rest -> do_scan(string.trim_left(rest), [token.Minimum, ..acc])
"max" <> rest -> do_scan(string.trim_left(rest), [token.Maximum, ..acc])

"&&" <> rest -> do_scan(string.trim_left(rest), [token.And, ..acc])
"||" <> rest -> do_scan(string.trim_left(rest), [token.Or, ..acc])
Expand Down
1 change: 1 addition & 0 deletions src/squared_away/squared_away_lang/scanner/token.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub type Token {
BuiltinAvg(key: option.Option(grid.GridKey))
MustBe
Minimum
Maximum

UpAnglePlus

Expand Down
Loading

0 comments on commit 333f2c0

Please sign in to comment.