lib/logic.nim
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import tables import ../core/types, ../core/parser, ../core/interpreter, ../core/utils # Comparison operators minsym ">", i: let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal > n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float > n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal > n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal > n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal > n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym ">=", i: let n2 = i.pop let n1 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float >= n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal >= n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal >= n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal >= n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "<", i: let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal > n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float > n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal > n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal > n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal > n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "<=", i: let n1 = i.pop let n2 = i.pop if n1.isNumber and n2.isNumber: if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: i.push newVal(n1.intVal.float >= n2.floatVal) elif n1.isFloat and n2.isFloat: i.push newVal(n1.floatVal >= n2.floatVal) elif n1.isFloat and n2.isInt: i.push newVal(n1.floatVal >= n2.intVal.float) elif n1.isString and n2.isString: i.push newVal(n1.strVal >= n2.strVal) else: i.error(errIncorrect, "Two numbers or two strings are required on the stack") minsym "==", i: let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol: i.push newVal(n1 == n2) else: i.error(errIncorrect, "Two non-symbol values of similar type are required") minsym "!=", i: let n2 = i.pop let n1 = i.pop if (n1.kind == n2.kind or (n1.isNumber and n2.isNumber)) and not n1.isSymbol: i.push newVal(not (n1 == n2)) else: i.error(errIncorrect, "Two non-symbol values of similar type are required") # Boolean Logic minsym "not", i: let b = i.pop if b.isBool: i.push newVal(not b.boolVal) else: i.error(errIncorrect, "A bool value is required on the stack") minsym "and", i: let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal and b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") minsym "or", i: let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal or b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") minsym "xor", i: let a = i.pop let b = i.pop if a.isBool and b.isBool: i.push newVal(a.boolVal xor b.boolVal) else: i.error(errIncorrect, "Two bool values are required on the stack") minsym "string?", i: if i.peek.kind == minString: i.push true.newVal else: i.push false.newVal minsym "int?", i: if i.peek.kind == minInt: i.push true.newVal else: i.push false.newVal minsym "float?", i: if i.peek.kind == minFloat: i.push true.newVal else: i.push false.newVal minsym "number?", i: if i.peek.kind == minFloat or i.peek.kind == minInt: i.push true.newVal else: i.push false.newVal minsym "bool?", i: if i.peek.kind == minBool: i.push true.newVal else: i.push false.newVal minsym "quotation?", i: if i.peek.kind == minQuotation: i.push true.newVal else: i.push false.newVal |