From d66d3bc13e41334c088710cf96b1b01adf1b88af Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 7 Nov 2024 16:15:37 -0600 Subject: [PATCH] update gh action --- .github/workflows/test.yml | 4 +- .../squared_away_lang/interpreter.gleam | 16 +++-- .../squared_away_lang/interpreter/value.gleam | 2 +- .../squared_away_lang/scanner.gleam | 68 ++++++++++++------- .../squared_away_lang/typechecker.gleam | 13 ++-- .../squared_away_lang/typechecker/typ.gleam | 1 - .../typechecker/typed_expr.gleam | 4 +- 7 files changed, 68 insertions(+), 40 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8bf9ad7..a4d0e13 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,5 +18,5 @@ jobs: gleam-version: "1.4.1" rebar3-version: "3" # elixir-version: "1.15.4" - - run: make fmt - - run: make test + - run: gleam format + - run: gleam test diff --git a/src/squared_away/squared_away_lang/interpreter.gleam b/src/squared_away/squared_away_lang/interpreter.gleam index 49a0c47..7ff576a 100644 --- a/src/squared_away/squared_away_lang/interpreter.gleam +++ b/src/squared_away/squared_away_lang/interpreter.gleam @@ -185,11 +185,15 @@ pub fn interpret( value.Integer(i), expr.Multiply, value.Usd(c) -> Ok(value.Usd(bigi.multiply(c, bigi.from_int(i)))) value.Usd(c), expr.Multiply, value.Percent(p) -> { - let cents = bigi.multiply(c, bigi.from_int(p)) |> bigi.divide(bigi.from_int(100)) + let cents = + bigi.multiply(c, bigi.from_int(p)) + |> bigi.divide(bigi.from_int(100)) Ok(value.Usd(cents)) } value.Percent(p), expr.Multiply, value.Usd(c) -> { - let cents = bigi.multiply(c, bigi.from_int(p)) |> bigi.divide(bigi.from_int(100)) + let cents = + bigi.multiply(c, bigi.from_int(p)) + |> bigi.divide(bigi.from_int(100)) Ok(value.Usd(cents)) } @@ -246,8 +250,12 @@ pub fn interpret( |> bigi.sum |> value.Usd |> Ok - _ -> - Error(error.RuntimeError(runtime_error.RuntimeError("internal compiler error sum function interpret"))) + _ -> + Error( + error.RuntimeError(runtime_error.RuntimeError( + "internal compiler error sum function interpret", + )), + ) } } } diff --git a/src/squared_away/squared_away_lang/interpreter/value.gleam b/src/squared_away/squared_away_lang/interpreter/value.gleam index e19d6cd..0344567 100644 --- a/src/squared_away/squared_away_lang/interpreter/value.gleam +++ b/src/squared_away/squared_away_lang/interpreter/value.gleam @@ -31,7 +31,7 @@ pub fn value_to_string(fv: Value) -> String { let cents = bigi.modulo(cents, bigi.from_int(100)) |> bigi.to_string let cents = case string.length(cents) { 1 -> cents <> "0" - 2 -> cents + 2 -> cents _ -> panic as "This shit shouldn't happen" } diff --git a/src/squared_away/squared_away_lang/scanner.gleam b/src/squared_away/squared_away_lang/scanner.gleam index d01ac99..a38a602 100644 --- a/src/squared_away/squared_away_lang/scanner.gleam +++ b/src/squared_away/squared_away_lang/scanner.gleam @@ -1,10 +1,10 @@ +import bigi import gleam/float import gleam/int import gleam/list import gleam/option import gleam/result import gleam/string -import bigi import squared_away/squared_away_lang/scanner/scan_error import squared_away/squared_away_lang/scanner/token @@ -20,11 +20,15 @@ pub fn scan(src: String) -> Result(List(token.Token), scan_error.ScanError) { // A formual starts with an = sign "=" <> rest -> do_scan(rest |> string.trim_left, []) - "$" <> rest -> case parse_usd_literal(rest) { - Error(e) -> Error(e) - Ok(#(n, "")) -> Ok([token.UsdLiteral(cents: n)]) - _ -> Error(scan_error.ScanError("Unexpected content after $ literal. If you're typing a formula, be sure to add an equal sign in front.")) - } + "$" <> rest -> + case parse_usd_literal(rest) { + Error(e) -> Error(e) + Ok(#(n, "")) -> Ok([token.UsdLiteral(cents: n)]) + _ -> + Error(scan_error.ScanError( + "Unexpected content after $ literal. If you're typing a formula, be sure to add an equal sign in front.", + )) + } txt -> { // We need to try and parse the text as a number literal @@ -42,20 +46,22 @@ pub fn scan(src: String) -> Result(List(token.Token), scan_error.ScanError) { // There should be an integer between 0 and 100 inclusive in front let percent = string.drop_right(txt, 1) case int.parse(percent) { - Ok(p) if p <= 100 && p >= 0 -> Ok([token.PercentLiteral(p)]) - Ok(_) | Error(_) -> Error(scan_error.ScanError("Percent literal must be an integer b/w 0 and 100 inclusive.")) + Ok(p) if p <= 100 && p >= 0 -> Ok([token.PercentLiteral(p)]) + Ok(_) | Error(_) -> + Error(scan_error.ScanError( + "Percent literal must be an integer b/w 0 and 100 inclusive.", + )) } } - False -> case parse_identifier(txt, "") { - Ok(#(ident, "")) -> Ok([token.LabelDef(ident)]) - _ -> - Error(scan_error.ScanError( - "Not a label definition, boolean, float, or integer. Are you possibly missing an `=` sign?", - )) - } + False -> + case parse_identifier(txt, "") { + Ok(#(ident, "")) -> Ok([token.LabelDef(ident)]) + _ -> + Error(scan_error.ScanError( + "Not a label definition, boolean, float, or integer. Are you possibly missing an `=` sign?", + )) + } } - - } } } @@ -81,7 +87,13 @@ fn parse_usd_literal( "Expected 2 decimal places following `.` character in usd literal", )) Ok(#(cents, rest)) if cents > 0 && cents < 100 -> - Ok(#(bigi.multiply(dollars, bigi.from_int(100) |> bigi.add(bigi.from_int(cents))), rest)) + Ok(#( + bigi.multiply( + dollars, + bigi.from_int(100) |> bigi.add(bigi.from_int(cents)), + ), + rest, + )) Ok(#(_, _)) -> Error(scan_error.ScanError( "Usd literal must have zero or two decimal places.", @@ -123,10 +135,12 @@ fn do_scan( "_" <> rest -> do_scan(string.trim_left(rest), [token.Underscore, ..acc]) "sum" <> rest -> do_scan(string.trim_left(rest), [token.BuiltinSum(option.None), ..acc]) - "$" <> rest -> case parse_usd_literal(rest) { - Error(e) -> Error(e) - Ok(#(cents, rest)) -> do_scan(string.trim_left(rest), [token.UsdLiteral(cents:), ..acc]) - } + "$" <> rest -> + case parse_usd_literal(rest) { + Error(e) -> Error(e) + Ok(#(cents, rest)) -> + do_scan(string.trim_left(rest), [token.UsdLiteral(cents:), ..acc]) + } _ -> { case parse_integer(src, "") { Ok(#(n, rest)) -> { @@ -144,7 +158,10 @@ fn do_scan( do_scan(string.trim_left(rest), [token.FloatLiteral(f), ..acc]) } "%" <> rest if n >= 0 && n <= 100 -> { - do_scan(string.trim_left(rest), [token.PercentLiteral(percent: n), ..acc]) + do_scan(string.trim_left(rest), [ + token.PercentLiteral(percent: n), + ..acc + ]) } _ -> do_scan(string.trim_left(rest), [token.IntegerLiteral(n), ..acc]) @@ -248,7 +265,10 @@ fn parse_integer(src: String, acc: String) -> Result(#(Int, String), Nil) { } } -fn parse_integer_text(src: String, acc: String) -> Result(#(String, String), Nil) { +fn parse_integer_text( + src: String, + acc: String, +) -> Result(#(String, String), Nil) { case src { "1" as x <> rest | "2" as x <> rest diff --git a/src/squared_away/squared_away_lang/typechecker.gleam b/src/squared_away/squared_away_lang/typechecker.gleam index 4b5e320..d4a2955 100644 --- a/src/squared_away/squared_away_lang/typechecker.gleam +++ b/src/squared_away/squared_away_lang/typechecker.gleam @@ -219,7 +219,8 @@ pub fn typecheck( Ok(typed_expr.BooleanLiteral(type_: typ.TBool, b:)) expr.FloatLiteral(f) -> Ok(typed_expr.FloatLiteral(type_: typ.TFloat, f:)) expr.UsdLiteral(cents) -> Ok(typed_expr.UsdLiteral(type_: typ.TUsd, cents:)) - expr.PercentLiteral(percent) -> Ok(typed_expr.PercentLiteral(type_: typ.TPercent, percent:)) + expr.PercentLiteral(percent) -> + Ok(typed_expr.PercentLiteral(type_: typ.TPercent, percent:)) expr.IntegerLiteral(n) -> Ok(typed_expr.IntegerLiteral(type_: typ.TInt, n:)) expr.Group(inner) -> { use expr <- result.try(typecheck(env, inner)) @@ -280,13 +281,13 @@ pub fn typecheck( Ok(typed_expr.BinaryOp(type_: typ.TFloat, lhs:, op:, rhs:)) typ.TInt, expr.Multiply, typ.TInt -> Ok(typed_expr.BinaryOp(type_: typ.TInt, lhs:, op:, rhs:)) - typ.TUsd, expr.Multiply, typ.TInt -> + typ.TUsd, expr.Multiply, typ.TInt -> Ok(typed_expr.BinaryOp(type_: typ.TUsd, lhs:, op:, rhs:)) - typ.TUsd, expr.Multiply, typ.TPercent -> + typ.TUsd, expr.Multiply, typ.TPercent -> Ok(typed_expr.BinaryOp(type_: typ.TUsd, lhs:, op:, rhs:)) - typ.TPercent, expr.Multiply, typ.TUsd -> + typ.TPercent, expr.Multiply, typ.TUsd -> Ok(typed_expr.BinaryOp(type_: typ.TUsd, lhs:, op:, rhs:)) - typ.TPercent, expr.Multiply, typ.TPercent -> + typ.TPercent, expr.Multiply, typ.TPercent -> Ok(typed_expr.BinaryOp(type_: typ.TPercent, lhs:, op:, rhs:)) typ.TPercent, expr.Multiply, some_type -> Ok(typed_expr.BinaryOp(type_: some_type, lhs:, op:, rhs:)) @@ -300,7 +301,7 @@ pub fn typecheck( Ok(typed_expr.BinaryOp(type_: typ.TInt, lhs:, op:, rhs:)) typ.TUsd, expr.Divide, typ.TUsd -> Ok(typed_expr.BinaryOp(type_: typ.TFloat, lhs:, op:, rhs:)) - typ.TUsd, expr.Divide, typ.TInt -> + typ.TUsd, expr.Divide, typ.TInt -> Ok(typed_expr.BinaryOp(type_: typ.TUsd, lhs:, op:, rhs:)) // Power diff --git a/src/squared_away/squared_away_lang/typechecker/typ.gleam b/src/squared_away/squared_away_lang/typechecker/typ.gleam index 8ec232d..e87befe 100644 --- a/src/squared_away/squared_away_lang/typechecker/typ.gleam +++ b/src/squared_away/squared_away_lang/typechecker/typ.gleam @@ -19,6 +19,5 @@ pub fn to_string(typ: Typ) { TTestResult -> "Test Result (Pass or Fail)" TUsd -> "Usd" TPercent -> "Percent" - } } diff --git a/src/squared_away/squared_away_lang/typechecker/typed_expr.gleam b/src/squared_away/squared_away_lang/typechecker/typed_expr.gleam index 25d211b..07a6a53 100644 --- a/src/squared_away/squared_away_lang/typechecker/typed_expr.gleam +++ b/src/squared_away/squared_away_lang/typechecker/typed_expr.gleam @@ -1,7 +1,7 @@ import bigi -import gleam/string import gleam/float import gleam/int +import gleam/string import squared_away/squared_away_lang/grid import squared_away/squared_away_lang/parser/expr import squared_away/squared_away_lang/typechecker/typ @@ -85,7 +85,7 @@ pub fn to_string(te: TypedExpr) -> String { let cents = bigi.modulo(cents, bigi.from_int(100)) |> bigi.to_string let cents = case string.length(cents) { 1 -> cents <> "0" - 2 -> cents + 2 -> cents _ -> panic as "This shit shouldn't happen" }