Skip to content

Commit

Permalink
update gh action
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Nov 7, 2024
1 parent a91282f commit d66d3bc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 12 additions & 4 deletions src/squared_away/squared_away_lang/interpreter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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",
)),
)
}
}
}
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 @@ -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"
}

Expand Down
68 changes: 44 additions & 24 deletions src/squared_away/squared_away_lang/scanner.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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?",
))
}
}


}
}
}
Expand All @@ -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.",
Expand Down Expand Up @@ -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)) -> {
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/squared_away/squared_away_lang/typechecker.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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:))
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/squared_away/squared_away_lang/typechecker/typ.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ pub fn to_string(typ: Typ) {
TTestResult -> "Test Result (Pass or Fail)"
TUsd -> "Usd"
TPercent -> "Percent"

}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}

Expand Down

0 comments on commit d66d3bc

Please sign in to comment.