aboutsummaryrefslogtreecommitdiffstats
path: root/lib/logic.moon
blob: 1c274dd877a277957fb8f035f73524007a4eb43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Op from require 'core'
unpack or= table.unpack

class BinOp extends Op
  setup: (...) =>
    @children = { ... }
    assert #@children >= 2, "#{@} needs at least two parameters"

  update: (dt) =>
    for child in *@children
      child\update dt

class eq extends BinOp
  @doc: "(eq a b [c]...)
(== a b [c]...) - check for equality"

  update: (dt) =>
    super\update dt

    @value = true
    val = @children[1]\get!
    for child in *@children[2,]
      @value and= val == child\get!


class and_ extends BinOp
  @doc: "(and a b [c]...) - AND values"

  update: (dt) =>
    super\update dt

    @value = true
    for child in *@children
      @value and= child\get!

class or_ extends BinOp
  @doc: "(or a b [c]...) - OR values

subtracts all other arguments from a"

  update: (dt) =>
    super\update dt

    @value = false
    for child in *@children
      @value or= child\get!

class not_ extends Op
  @doc: "(not a) - boolean opposite"

  setup: (@a) =>

  update: (dt) =>
    @a\update dt

    @value = not @a\get!

class bool extends Op
  @doc: "(bool a) - convert to bool"

  setup: (@a) =>

  update: (dt) =>
    @a\update dt

    @value = switch @a\get!
      when false, nil, 0
        false
      else
        true

{
  '==': eq
  :eq
  and: and_
  or: or_
  not: not_
  :bool
}