Continue MARK AS updates...

mark_as
Rick Carlino 2020-04-29 19:04:46 -05:00
parent 816da6128a
commit 75bbeb7281
5 changed files with 51 additions and 22 deletions

View File

@ -65,11 +65,12 @@ defmodule FarmbotCeleryScript.Compiler do
end
def compile(%AST{kind: kind} = ast, env) when kind in @valid_entry_points do
raise "This is the entrypoint to everything... Figure out where :name, :x, :y, :z come from..."
compile_entry_point(compile_ast(ast, env), env, [])
end
def compile_entry_point([{_, new_env, _} = compiled | rest], env, acc) do
env = Keyword.merge(env, new_env)
def compile_entry_point([{_, new_env, _} = compiled | rest], old_env, acc) do
env = Keyword.merge(old_env, new_env)
debug_mode?() && print_compiled_code(compiled)
# entry points must be evaluated once more with the calling `env`
# to return a list of compiled `steps`
@ -104,6 +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(ast, env), to: Compiler.DataControl
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

View File

@ -1,6 +1,19 @@
defmodule FarmbotCeleryScript.Compiler.DataControl do
alias FarmbotCeleryScript.Compiler
def resource(ast, _env) do
IO.puts("======")
IO.inspect(ast)
# %FarmbotCeleryScript.AST{
# args: %{resource_id: 0, resource_type: "Device"},
# body: [],
# comment: nil,
# kind: :resource,
# meta: nil
# }
raise "TODO: Pull resource from DB?"
end
# compiles coordinate
# Coordinate should return a vec3
def coordinate(%{args: %{x: x, y: y, z: z}}, env) do

View File

@ -1,27 +1,41 @@
defmodule FarmbotCeleryScript.Compiler.UpdateResource do
alias FarmbotCeleryScript.Compiler
def update_resource(ast, env) do
resource = Map.fetch!(ast.args, :resource)
alias FarmbotCeleryScript.{Compiler, AST}
def update_resource(%AST{args: args, body: body}, env) do
quote do
p = unquote(destructure_pairs(ast.body, %{}))
IO.inspect(p, label: "params")
result = Compiler.compile_ast(unquote(resource), unquote(env))
FarmbotCeleryScript.SysCalls.update_resource(result.kind, result.id, p)
unquote(__MODULE__).do_update(
unquote(Map.fetch!(args, :resource)),
unquote(unpair(body, %{})),
unquote(env)
)
end
end
defp destructure_pairs([pair | rest], acc) do
def do_update(%AST{kind: :identifier} = res, update, env) do
{name, environ, nil} = Compiler.compile_ast(res, env)
value = Keyword.fetch!(environ, name)
IO.inspect(%{res: res, update: update, value: value}, label: "QQQ")
raise "Update sequence compiler. `value` missing resource kind / ID"
end
def do_update(%AST{kind: :resource} = res, update, _) do
%{resource_id: id, resource_type: kind} = res.args
FarmbotCeleryScript.SysCalls.update_resource(kind, id, update)
end
def do_update(res, _, _) do
raise "update_resource error. Please notfiy support: #{inspect(res)}"
end
defp unpair([pair | rest], acc) do
IO.puts("TODO: Need to apply handlebars to `value`s.")
key = Map.fetch!(pair.args, :label)
val = Map.fetch!(pair.args, :value)
next_acc = Map.merge(acc, %{key => val})
destructure_pairs(rest, next_acc)
unpair(rest, next_acc)
end
defp destructure_pairs([], acc) do
defp unpair([], acc) do
acc
end
end

View File

@ -8,7 +8,7 @@ defmodule FarmbotOS.SysCalls.PointLookup do
def point(kind, id) do
case Asset.get_point(id: id) do
nil -> {:error, "#{kind} not found"}
nil -> {:error, "#{kind || "point?"} #{id} not found"}
%{name: name, x: x, y: y, z: z} -> %{name: name, x: x, y: y, z: z}
end
end

View File

@ -14,14 +14,14 @@ defmodule FarmbotOS.SysCalls.ResourceUpdateTest do
end)
end
test "resource_update/3 - Device" do
test "update_resource/3 - Device" do
fake_coords!()
params = %{name: "X is {{ x }}"}
assert :ok == ResourceUpdate.resource_update("Device", 0, params)
assert :ok == ResourceUpdate.update_resource("Device", 0, params)
assert "X is 1.2" == FarmbotCore.Asset.device().name
end
test "resource_update/3 - Point" do
test "update_resource/3 - Point" do
Repo.delete_all(Point)
%Point{id: 555, pointer_type: "Plant"}
@ -29,17 +29,17 @@ defmodule FarmbotOS.SysCalls.ResourceUpdateTest do
|> Repo.insert!()
params = %{name: "Updated to {{ x }}"}
assert :ok == ResourceUpdate.resource_update("Plant", 555, params)
assert :ok == ResourceUpdate.update_resource("Plant", 555, params)
next_plant = PointLookup.point("Plant", 555)
assert "Updated to " == next_plant.name
bad_result1 = ResourceUpdate.resource_update("Plant", 0, params)
bad_result1 = ResourceUpdate.update_resource("Plant", 0, params)
error = "Plant.0 is not currently synced, so it could not be updated"
assert {:error, error} == bad_result1
end
test "resource_update/3 - unknown" do
{:error, error} = ResourceUpdate.resource_update("Foo", 0, nil)
test "update_resource/3 - unknown" do
{:error, error} = ResourceUpdate.update_resource("Foo", 0, nil)
assert error == "Unknown resource: Foo.0\n"
end
end