WIP
parent
c7653e14a0
commit
856e19147e
|
@ -105,7 +105,7 @@ defmodule FarmbotCeleryScript.Compiler do
|
|||
defdelegate named_pin(ast, env), to: Compiler.DataControl
|
||||
defdelegate point(ast, env), to: Compiler.DataControl
|
||||
defdelegate read_pin(ast, env), to: Compiler.PinControl
|
||||
defdelegate resource_update(ast, env), to: Compiler.DataControl
|
||||
defdelegate update_resource(ast, env), to: Compiler.UpdateResource
|
||||
defdelegate rpc_request(ast, env), to: Compiler.RPCRequest
|
||||
defdelegate sequence(ast, env), to: Compiler.Sequence
|
||||
defdelegate set_pin_io_mode(ast, env), to: Compiler.PinControl
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
defmodule FarmbotCeleryScript.Compiler.DataControl do
|
||||
alias FarmbotCeleryScript.Compiler
|
||||
|
||||
# compiles coordinate
|
||||
# Coordinate should return a vec3
|
||||
def coordinate(%{args: %{x: x, y: y, z: z}}, env) do
|
||||
quote location: :keep do
|
||||
FarmbotCeleryScript.SysCalls.coordinate(
|
||||
unquote(Compiler.compile_ast(x, env)),
|
||||
unquote(Compiler.compile_ast(y, env)),
|
||||
unquote(Compiler.compile_ast(z, env))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# compiles point
|
||||
def point(%{args: %{pointer_type: type, pointer_id: id}}, env) do
|
||||
quote location: :keep do
|
||||
FarmbotCeleryScript.SysCalls.point(
|
||||
unquote(Compiler.compile_ast(type, env)),
|
||||
unquote(Compiler.compile_ast(id, env))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# compile a named pin
|
||||
def named_pin(%{args: %{pin_id: id, pin_type: type}}, env) do
|
||||
quote location: :keep do
|
||||
FarmbotCeleryScript.SysCalls.named_pin(
|
||||
unquote(Compiler.compile_ast(type, env)),
|
||||
unquote(Compiler.compile_ast(id, env))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def tool(%{args: %{tool_id: tool_id}}, env) do
|
||||
quote location: :keep do
|
||||
FarmbotCeleryScript.SysCalls.get_toolslot_for_tool(
|
||||
unquote(Compiler.compile_ast(tool_id, env))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def resource_update(
|
||||
%{
|
||||
args: %{
|
||||
resource_type: kind,
|
||||
resource_id: id,
|
||||
label: label,
|
||||
value: value
|
||||
},
|
||||
body: body
|
||||
},
|
||||
env
|
||||
) do
|
||||
initial = %{label => value}
|
||||
# Technically now body isn't supported by this node.
|
||||
extra =
|
||||
Map.new(body, fn %{args: %{label: label, data_value: value}} ->
|
||||
{label, value}
|
||||
end)
|
||||
|
||||
# Make sure the initial stuff higher most priority
|
||||
params = Map.merge(extra, initial)
|
||||
|
||||
quote do
|
||||
FarmbotCeleryScript.SysCalls.resource_update(
|
||||
unquote(Compiler.compile_ast(kind, env)),
|
||||
unquote(Compiler.compile_ast(id, env)),
|
||||
unquote(Macro.escape(params))
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,31 @@
|
|||
defmodule FarmbotCeleryScript.Compiler.UpdateResource do
|
||||
# alias FarmbotCeleryScript.Compiler
|
||||
|
||||
def coordinate(_ast, _env) do
|
||||
raise "TODO: coordinate"
|
||||
end
|
||||
|
||||
def named_pin(_ast, _env) do
|
||||
raise "TODO: named_pin"
|
||||
end
|
||||
|
||||
def point(_ast, _env) do
|
||||
raise "TODO: point"
|
||||
end
|
||||
|
||||
def tool(_ast, _env) do
|
||||
raise "TODO: tool"
|
||||
end
|
||||
|
||||
def update_resource(_ast, _env) do
|
||||
raise "Its here!"
|
||||
|
||||
quote do
|
||||
# FarmbotCeleryScript.SysCalls.update_resource(
|
||||
# unquote(Compiler.compile_ast(kind, env)),
|
||||
# unquote(Compiler.compile_ast(id, env)),
|
||||
# unquote(Macro.escape(params))
|
||||
# )
|
||||
end
|
||||
end
|
||||
end
|
|
@ -69,7 +69,6 @@ defmodule FarmbotCeleryScript.SysCalls do
|
|||
@callback toggle_pin(pin_num :: number()) :: ok_or_error
|
||||
@callback read_status() :: ok_or_error
|
||||
@callback reboot() :: ok_or_error
|
||||
@callback resource_update(String.t(), resource_id, map()) :: ok_or_error
|
||||
@callback send_message(type :: String.t(), message :: String.t(), [atom]) ::
|
||||
ok_or_error
|
||||
@callback set_servo_angle(pin :: number(), value :: number()) :: ok_or_error
|
||||
|
@ -297,10 +296,6 @@ defmodule FarmbotCeleryScript.SysCalls do
|
|||
ok_or_error(sys_calls, :reboot, [])
|
||||
end
|
||||
|
||||
def resource_update(sys_calls \\ @sys_calls, kind, id, params) do
|
||||
ok_or_error(sys_calls, :resource_update, [kind, id, params])
|
||||
end
|
||||
|
||||
def send_message(sys_calls \\ @sys_calls, kind, msg, channels) do
|
||||
ok_or_error(sys_calls, :send_message, [kind, msg, channels])
|
||||
end
|
||||
|
|
|
@ -119,10 +119,6 @@ defmodule FarmbotCeleryScript.SysCalls.Stubs do
|
|||
@impl true
|
||||
def reboot(), do: error(:reboot, [])
|
||||
|
||||
@impl true
|
||||
def resource_update(kind, resource_id, data),
|
||||
do: error(:resource_update, [kind, resource_id, data])
|
||||
|
||||
@impl true
|
||||
def send_message(type, message, channels),
|
||||
do: error(:send_message, [type, message, channels])
|
||||
|
|
|
@ -355,6 +355,16 @@ defmodule FarmbotCeleryScript.CompilerTest do
|
|||
""")
|
||||
end
|
||||
|
||||
test "`update_resource`: Multiple fields of `resource` type." do
|
||||
compiled = "test/fixtures/update_resource_multi.json"
|
||||
|> File.read!()
|
||||
|> Jason.decode!()
|
||||
|> AST.decode()
|
||||
|> compile()
|
||||
|
||||
assert compiled == strip_nl("?")
|
||||
end
|
||||
|
||||
defp compile(ast) do
|
||||
ast
|
||||
|> Compiler.compile_ast([])
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule FarmbotCeleryScript.Corpus.NodeTest do
|
|||
|
||||
test "inspect" do
|
||||
a =
|
||||
"Sequence(version, locals) [calibrate, change_ownership, check_updates, emergency_lock, emergency_unlock, execute, execute_script, factory_reset, find_home, flash_firmware, home, install_farmware, install_first_party_farmware, _if, move_absolute, move_relative, power_off, read_pin, read_status, reboot, remove_farmware, resource_update, send_message, set_servo_angle, set_user_env, sync, take_photo, toggle_pin, update_farmware, wait, write_pin, zero]"
|
||||
"Sequence(version, locals) [calibrate, change_ownership, check_updates, emergency_lock, emergency_unlock, execute, execute_script, factory_reset, find_home, flash_firmware, home, install_farmware, install_first_party_farmware, _if, move_absolute, move_relative, power_off, read_pin, read_status, reboot, remove_farmware, update_resource, send_message, set_servo_angle, set_user_env, sync, take_photo, toggle_pin, update_farmware, wait, write_pin, zero]"
|
||||
|
||||
b = inspect(Corpus.sequence())
|
||||
assert a == b
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
{
|
||||
"args": {
|
||||
"locals": {
|
||||
"args": {},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"default_value": {
|
||||
"args": {
|
||||
"x": 1,
|
||||
"y": 2,
|
||||
"z": 3
|
||||
},
|
||||
"kind": "coordinate"
|
||||
},
|
||||
"label": "parent"
|
||||
},
|
||||
"kind": "parameter_declaration"
|
||||
}
|
||||
],
|
||||
"kind": "scope_declaration"
|
||||
},
|
||||
"version": -999
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"resource": {
|
||||
"args": {
|
||||
"label": "parent"
|
||||
},
|
||||
"kind": "identifier"
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"label": "meta.my_prop",
|
||||
"value": "whatever"
|
||||
},
|
||||
"kind": "pair"
|
||||
}
|
||||
],
|
||||
"kind": "update_resource"
|
||||
}
|
||||
],
|
||||
"kind": "sequence"
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
{
|
||||
"args": {
|
||||
"locals": {
|
||||
"args": {},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"default_value": {
|
||||
"args": {
|
||||
"x": 1,
|
||||
"y": 2,
|
||||
"z": 3
|
||||
},
|
||||
"kind": "coordinate"
|
||||
},
|
||||
"label": "parent"
|
||||
},
|
||||
"kind": "parameter_declaration"
|
||||
}
|
||||
],
|
||||
"kind": "scope_declaration"
|
||||
},
|
||||
"version": -999
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"resource": {
|
||||
"args": {
|
||||
"label": "parent"
|
||||
},
|
||||
"kind": "identifier"
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"label": "plant_stage",
|
||||
"value": "removed"
|
||||
},
|
||||
"kind": "pair"
|
||||
}
|
||||
],
|
||||
"kind": "update_resource"
|
||||
}
|
||||
],
|
||||
"kind": "sequence"
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"args": {
|
||||
"locals": {
|
||||
"args": {},
|
||||
"body": [],
|
||||
"kind": "scope_declaration"
|
||||
},
|
||||
"version": -999
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"resource": {
|
||||
"args": {
|
||||
"resource_id": 0,
|
||||
"resource_type": "Device"
|
||||
},
|
||||
"kind": "resource"
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"label": "mounted_tool_id",
|
||||
"value": 12161
|
||||
},
|
||||
"kind": "pair"
|
||||
}
|
||||
],
|
||||
"kind": "update_resource"
|
||||
}
|
||||
],
|
||||
"kind": "sequence"
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"args": {
|
||||
"locals": {
|
||||
"args": {},
|
||||
"body": [],
|
||||
"kind": "scope_declaration"
|
||||
},
|
||||
"version": -999
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"resource": {
|
||||
"args": {
|
||||
"resource_id": 23,
|
||||
"resource_type": "Plant"
|
||||
},
|
||||
"kind": "resource"
|
||||
}
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"args": {
|
||||
"label": "plant_stage",
|
||||
"value": "planted"
|
||||
},
|
||||
"kind": "pair"
|
||||
},
|
||||
{
|
||||
"args": {
|
||||
"label": "r",
|
||||
"value": 23
|
||||
},
|
||||
"kind": "pair"
|
||||
}
|
||||
],
|
||||
"kind": "update_resource"
|
||||
}
|
||||
],
|
||||
"kind": "sequence"
|
||||
}
|
Loading…
Reference in New Issue