txlyre 17 hours ago
parent
commit
bde13c64b9
2 changed files with 735 additions and 126 deletions
  1. 602 125
      qistd.c
  2. 133 1
      std.qi

File diff suppressed because it is too large
+ 602 - 125
qistd.c


+ 133 - 1
std.qi

@@ -535,4 +535,136 @@ class Error {
 class AssertionError(Error)
 func assert(cond, msg=AssertionError())
   if !cond
-    throw msg
+    throw msg
+class OrderedTable {
+    __data__ = nil
+
+    constructor (this, v=nil) {
+        this.__data__ = []
+
+        if v !is nil {
+            v = table(v)
+
+            for var k of v
+                this[k] = v[k]
+        }
+    }
+
+    __type (this): "orderedtable"
+    
+    stringify (this, tempstack=[]) {
+        if this in tempstack
+            return "OrderedTable({...})"
+
+        tempstack.push(this)
+
+        var buf = "{"
+        var f = true
+
+        for var [k, v] of this.__data__ {
+            if f
+                f = false
+            else buf += ", "
+
+            k = k.replace(`\`, `\\`).replace(`"`, `\"`)
+
+            if type(v) is "orderedtable"
+                v = v.stringify(tempstack)
+            
+            buf += f`"${k}": ${v}`
+        }
+
+        buf += "}"
+
+        return buf
+    }
+
+    __str (this) {
+        return this.stringify([])
+    }
+
+    keys (this) {
+        let keys = []
+
+        for var [k, _] of this.__data__
+            keys.push(k)
+
+        return keys
+    }
+
+    values (this) {
+        let values = []
+
+        for var [_, v] of this.__data__
+            values.push(v)
+
+        return values
+    }
+
+    delete (this, k) {
+        del this[k]
+    }
+
+    copy (this) {
+        return OrderedTable(this.__data__)
+    }
+
+    get (this, k, d=nil) {
+        k = str(k)
+
+        for var [ok, v] of this.__data__
+            if ok == k
+                return v
+
+        return d
+    }
+
+    __iter (this): this.keys()
+    __len (this): len(this.__data__)
+
+    __in (this, k) {
+        k = str(k)
+
+        for var [ok, _] of this.__data__
+            if ok == k
+                return true
+
+        return false
+    }
+
+    __del (this, k) {
+        k = str(k)
+
+        for var [i, ok] of zip(range(len(this)), this.keys())
+            if ok == k {
+                del this.__data__[i]
+
+                return
+            }
+
+        throw "no such key: " + k
+    }
+
+    __index (this, k) {
+        k = str(k)
+
+        for var [ok, v] of this.__data__
+            if ok == k
+                return v
+
+        throw "no such key: " + k
+    }
+
+    __index_set (this, k, v) {
+        k = str(k)
+
+        for var [i, ok] of zip(range(len(this)), this.keys())
+            if ok == k {
+                this.__data__[i] = (ok, v)
+
+                return
+            }
+
+        this.__data__.push((k, v))
+    }
+}

Some files were not shown because too many files changed in this diff