Skip to content

Commit

Permalink
Merge pull request roc-lang#7369 from roc-lang/fix-false
Browse files Browse the repository at this point in the history
Fix false interpreter
  • Loading branch information
bhansconnect authored Dec 16, 2024
2 parents 32e0ea3 + b9269be commit e819c95
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 564 deletions.
44 changes: 24 additions & 20 deletions crates/cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,31 +212,34 @@ mod cli_tests {
);
}

// TODO check this out, there's more that's going wrong than a segfault
//#[test]
/*#[cfg_attr(
any(target_os = "windows", target_os = "linux", target_os = "macos"),
ignore = "Segfault, likely broken because of alias analysis: https://github.com/roc-lang/roc/issues/6544"
)]*/
/*
#[test]
#[cfg_attr(windows, ignore)]
fn false_interpreter() {
let cli_build = ExecCli::new(
CMD_BUILD,
file_from_root("crates/cli/tests/test-projects/false-interpreter", "main.roc")
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);
CMD_BUILD,
file_from_root(
"crates/cli/tests/test-projects/false-interpreter",
"main.roc",
),
)
.arg(BUILD_HOST_FLAG)
.arg(SUPPRESS_BUILD_HOST_WARNING_FLAG);

let sqrt_false_path_buf = file_from_root("crates/cli/tests/test-projects/false-interpreter/examples", "sqrt.false");
let sqrt_false_path_buf = file_from_root(
"crates/cli/tests/test-projects/false-interpreter/examples",
"sqrt.false",
);

let app_args = ["--",
sqrt_false_path_buf
.as_path()
.to_str()
.unwrap()];
let app_args = [sqrt_false_path_buf.as_path().to_str().unwrap()];

cli_build.full_check_build_and_run("1414", TEST_LEGACY_LINKER, ALLOW_VALGRIND, None, Some(&app_args));
}*/
cli_build.full_check_build_and_run(
"1414",
TEST_LEGACY_LINKER,
ALLOW_VALGRIND,
None,
Some(&app_args),
);
}

#[test]
#[cfg_attr(windows, ignore)]
Expand Down Expand Up @@ -1302,6 +1305,7 @@ mod cli_tests {
}

#[test]
#[ignore = "flaky currently due to 7022"]
fn known_type_error() {
let cli_check = ExecCli::new(
CMD_CHECK,
Expand Down
36 changes: 18 additions & 18 deletions crates/cli/tests/test-projects/false-interpreter/Context.roc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module [Context, Data, with, getChar, Option, pushStack, popStack, toStr, inWhileScope]
module [Context, Data, with!, getChar!, Option, pushStack, popStack, toStr, inWhileScope]

import pf.File
import Variable exposing [Variable]
Expand All @@ -20,13 +20,13 @@ pushStack = \ctx, data ->
# I think an open tag union should just work here.
# Instead at a call sites, I need to match on the error and then return the same error.
# Otherwise it hits unreachable code in ir.rs
popStack : Context -> Result [T Context Data] [EmptyStack]
popStack : Context -> Result (Context, Data) [EmptyStack]
popStack = \ctx ->
when List.last ctx.stack is
Ok val ->
poppedCtx = { ctx & stack: List.dropAt ctx.stack (List.len ctx.stack - 1) }

Ok (T poppedCtx val)
Ok (poppedCtx, val)

Err ListWasEmpty ->
Err EmptyStack
Expand Down Expand Up @@ -58,30 +58,30 @@ toStr = \{ scopes, stack, state, vars } ->

"\n============\nDepth: $(depth)\nState: $(stateStr)\nStack: [$(stackStr)]\nVars: [$(varsStr)]\n============\n"

with : Str, (Context -> Task {} a) -> Task {} a
with = \path, callback ->
File.withOpen path \handle ->
with! : Str, (Context => a) => a
with! = \path, callback! ->
File.withOpen! path \handle ->
# I cant define scope here and put it in the list in callback. It breaks alias anaysis.
# Instead I have to inline this.
# root_scope = { data: Some handle, index: 0, buf: [], whileInfo: None }
callback { scopes: [{ data: Some handle, index: 0, buf: [], whileInfo: None }], state: Executing, stack: [], vars: List.repeat (Number 0) Variable.totalCount }
callback! { scopes: [{ data: Some handle, index: 0, buf: [], whileInfo: None }], state: Executing, stack: [], vars: List.repeat (Number 0) Variable.totalCount }

# I am pretty sure there is a syntax to destructure and keep a reference to the whole, but Im not sure what it is.
getChar : Context -> Task [T U8 Context] [EndOfData, NoScope]
getChar = \ctx ->
getChar! : Context => Result (U8, Context) [EndOfData, NoScope]
getChar! = \ctx ->
when List.last ctx.scopes is
Ok scope ->
(T val newScope) = getCharScope! scope
Task.ok (T val { ctx & scopes: List.set ctx.scopes (List.len ctx.scopes - 1) newScope })
(val, newScope) = getCharScope!? scope
Ok (val, { ctx & scopes: List.set ctx.scopes (List.len ctx.scopes - 1) newScope })

Err ListWasEmpty ->
Task.err NoScope
Err NoScope

getCharScope : Scope -> Task [T U8 Scope] [EndOfData, NoScope]
getCharScope = \scope ->
getCharScope! : Scope => Result (U8, Scope) [EndOfData, NoScope]
getCharScope! = \scope ->
when List.get scope.buf scope.index is
Ok val ->
Task.ok (T val { scope & index: scope.index + 1 })
Ok (val, { scope & index: scope.index + 1 })

Err OutOfBounds ->
when scope.data is
Expand All @@ -90,13 +90,13 @@ getCharScope = \scope ->
when List.first bytes is
Ok val ->
# This starts at 1 because the first character is already being returned.
Task.ok (T val { scope & buf: bytes, index: 1 })
Ok (val, { scope & buf: bytes, index: 1 })

Err ListWasEmpty ->
Task.err EndOfData
Err EndOfData

None ->
Task.err EndOfData
Err EndOfData

inWhileScope : Context -> Bool
inWhileScope = \ctx ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ fromUtf8 = \char ->
toIndex : Variable -> U64
toIndex = \@Variable char ->
Num.intCast (char - 0x61) # "a"
# List.first (Str.toUtf8 "a")
Loading

0 comments on commit e819c95

Please sign in to comment.