Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thing #246

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/elm/Compiler/Ast.elm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Node
| Run Node
| Make Node Node
| Localmake Node Node
| Thing Node
| Variable String
| Value Type.Value
| Raise Exception
Expand Down Expand Up @@ -205,6 +206,9 @@ typeOfCallee node =
Localmake _ _ ->
Command { name = "localmake" }

Thing _ ->
Primitive { name = "thing" }

Variable name ->
Primitive { name = name }

Expand Down Expand Up @@ -786,6 +790,12 @@ compile context node =
]
|> List.concat

Thing name ->
[ compileInContext (Expression { caller = "thing" }) name
, [ Instruction.Thing ]
]
|> List.concat

Variable name ->
[ PushVariable name ]

Expand Down
11 changes: 11 additions & 0 deletions app/elm/Compiler/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ statement state =
, localmake state
, make state
, templateVariable
, thing state
, variable
, P.lazy (\_ -> functionCall state)
, Value.value
Expand Down Expand Up @@ -702,6 +703,16 @@ make state =
)


thing : State -> Parser Ast.Node
thing state =
P.inContext Thing <|
(P.succeed Ast.Thing
|. Helper.keyword "thing"
|. Helper.spaces
|= booleanExpression state
)


variable : Parser Ast.Node
variable =
P.inContext Variable <|
Expand Down
1 change: 1 addition & 0 deletions app/elm/Compiler/Parser/Context.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Context
| TemplateVariable
| Localmake
| Make
| Thing
| Variable
| ValueOutsideList
| ValueInList
Expand Down
1 change: 1 addition & 0 deletions app/elm/Vm/Instruction.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Instruction
| LocalVariable String
| Make
| Localmake
| Thing
| Introspect0 I.Introspect0
| Introspect1 I.Introspect1
| Eval
Expand Down
30 changes: 30 additions & 0 deletions app/elm/Vm/Vm.elm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ encodeInstruction instruction =
Localmake ->
"Localmake"

Thing ->
"Thing"

Introspect0 { name } ->
"Introspect0 " ++ name

Expand Down Expand Up @@ -812,6 +815,30 @@ localmake vm =
)


thing : Vm -> Result Error Vm
thing vm =
popValue1 vm
|> Result.andThen
(\( name, newVm ) ->
name
|> Type.toWord
|> Result.mapError (\_ -> WrongInput "thing" (Type.toDebugString name))
|> Result.andThen
(\word ->
case Scope.thing word vm.scopes of
Just (Defined value) ->
Ok
(newVm
|> pushValue1 value
|> incrementProgramCounter
)

_ ->
Err <| Error.VariableUndefined word
)
)


pushLoopScope : Vm -> Result Error Vm
pushLoopScope vm =
popValue1 vm
Expand Down Expand Up @@ -1061,6 +1088,9 @@ execute instruction vm =
Localmake ->
localmake vm

Thing ->
thing vm

Introspect0 primitive ->
introspect0 primitive vm

Expand Down
11 changes: 10 additions & 1 deletion tests/Test/Run/Builtin.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Test.Run.Builtin exposing (commands, make, primitives, print, show)
module Test.Run.Builtin exposing (commands, make, primitives, print, show, thing)

import Test exposing (Test, describe)
import Test.Helper exposing (printsLine, runsWithoutError)
Expand Down Expand Up @@ -179,3 +179,12 @@ make =
, printsLine "make 1.1 1 print :1.1" "1"
, printsLine "make 1.1 1 make :1.1 2 print :1" "2"
]


thing : Test
thing =
describe "thing" <|
[ printsLine "make \"a \"word print thing \"a" "word"
, printsLine "make \"a \"word make \"b \"a print thing :b" "word"
, printsLine "make \"a \"word make \"b \"a print thing thing \"b" "word"
]