Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MoveTurtle(ByVal cb As uCalc.Callback)
      Dim dist = cb.Arg(1)
      Dim uc_inst = cb.uCalc
      Dim x = uc_inst.ItemOf("x").Value()
      Dim y = uc_inst.ItemOf("y").Value()
      Dim angle = uc_inst.ItemOf("angle").Value()
      Dim pen_down = uc_inst.ItemOf("pen_down").ValueBool()
      Dim new_x As Integer = Convert.ToInt32(x + dist * uc_inst.Eval("CosD(" + (angle).ToString() + ")"))
      Dim new_y As Integer = Convert.ToInt32(y + dist * uc_inst.Eval("SinD(" + (angle).ToString() + ")"))
      If pen_down Then
         Console.WriteLine($"Drawing line to ({new_x},{new_y})")
      Else
         Console.WriteLine($"Moving to ({new_x},{new_y})")
      End If
      uc_inst.ItemOf("x").Value(new_x)
      uc_inst.ItemOf("y").Value(new_y)
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// --- Setup ---
      uc.DefineVariable("x = 0.0")
      uc.DefineVariable("y = 0.0")
      uc.DefineVariable("angle = 90.0")
      uc.DefineVariable("pen_down = false")
      uc.DefineConstant("PI = 3.1415926535")
      uc.DefineFunction("CosD(a) = Cos(a * PI / 180)")
      uc.DefineFunction("SinD(a) = Sin(a * PI / 180)")
      uc.DefineFunction("Move(dist)", AddressOf MoveTurtle)
      
      Dim t = uc.ExpressionTransformer
      t.FromTo("FD {@Number:dist}", "Move({dist})")
      t.FromTo("RT {@Number:deg}", "angle = angle - {deg}")
      t.FromTo("PD", "pen_down = true")
      
      '// --- Script ---
      Dim script = "
PD
FD 100
RT 90
FD 50
"
      
      uc.EvalStr(script)
      
      Console.WriteLine($"Final Position: ({uc.Eval("x")}, {uc.Eval("y")})")
   End Sub
End Module