mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 08:56:07 +00:00
make maps oop
This commit is contained in:
151
src/map.go
151
src/map.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mapCompiled = makeRegex(`( *)\{(((( *).+( *):( *).+( *))|(` + spacelessVariable + `))(( *)\,(( *).+( *):( *).+( *))|(` + spacelessVariable + `)))*\}( *)`)
|
||||
@@ -25,16 +26,146 @@ func parseMap(code UNPARSEcode) (any, UNPARSEcode) {
|
||||
return nil, UNPARSEcode{}
|
||||
}
|
||||
|
||||
func Map(val anymap) ArObject {
|
||||
func Map(m anymap) ArObject {
|
||||
var mutex = sync.RWMutex{}
|
||||
return ArObject{
|
||||
TYPE: "map",
|
||||
obj: val,
|
||||
}
|
||||
}
|
||||
|
||||
func Class(val anymap) ArObject {
|
||||
return ArObject{
|
||||
TYPE: "class",
|
||||
obj: val,
|
||||
obj: anymap{
|
||||
"__value__": m,
|
||||
"__name__": "map",
|
||||
"get": builtinFunc{
|
||||
"get",
|
||||
func(args ...any) (any, ArErr) {
|
||||
if len(args) < 1 || len(args) > 2 {
|
||||
return nil, ArErr{
|
||||
TYPE: "Runtime Error",
|
||||
message: "expected 1 or 2 argument, got " + fmt.Sprint(len(args)),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
var DEFAULT any
|
||||
key := ArValidToAny(args[0])
|
||||
if isUnhashable(key) {
|
||||
return nil, ArErr{
|
||||
TYPE: "Runtime Error",
|
||||
message: "unhashable type: " + typeof(key),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
if len(args) == 2 {
|
||||
DEFAULT = (args[1])
|
||||
}
|
||||
mutex.RLock()
|
||||
if _, ok := m[key]; !ok {
|
||||
mutex.RUnlock()
|
||||
return DEFAULT, ArErr{}
|
||||
}
|
||||
v := m[key]
|
||||
mutex.RUnlock()
|
||||
return v, ArErr{}
|
||||
},
|
||||
},
|
||||
"__Contains__": builtinFunc{
|
||||
"__Contains__",
|
||||
func(args ...any) (any, ArErr) {
|
||||
if len(args) != 1 {
|
||||
return nil, ArErr{
|
||||
TYPE: "TypeError",
|
||||
message: "expected 1 argument, got " + fmt.Sprint(len(args)),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
key := ArValidToAny(args[0])
|
||||
if isUnhashable(key) {
|
||||
return false, ArErr{}
|
||||
}
|
||||
mutex.RLock()
|
||||
if _, ok := m[key]; !ok {
|
||||
mutex.RUnlock()
|
||||
return false, ArErr{}
|
||||
}
|
||||
mutex.RUnlock()
|
||||
return true, ArErr{}
|
||||
},
|
||||
},
|
||||
"__NotContains__": builtinFunc{
|
||||
"__NotContains__",
|
||||
func(args ...any) (any, ArErr) {
|
||||
if len(args) != 1 {
|
||||
return nil, ArErr{
|
||||
TYPE: "TypeError",
|
||||
message: "expected 1 argument, got " + fmt.Sprint(len(args)),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
key := ArValidToAny(args[0])
|
||||
if isUnhashable(key) {
|
||||
return true, ArErr{}
|
||||
}
|
||||
mutex.RLock()
|
||||
if _, ok := m[key]; !ok {
|
||||
mutex.RUnlock()
|
||||
return true, ArErr{}
|
||||
}
|
||||
mutex.RUnlock()
|
||||
return false, ArErr{}
|
||||
},
|
||||
},
|
||||
"__setindex__": builtinFunc{
|
||||
"__setindex__",
|
||||
func(args ...any) (any, ArErr) {
|
||||
if len(args) != 2 {
|
||||
return nil, ArErr{
|
||||
TYPE: "TypeError",
|
||||
message: "expected 2 arguments, got " + fmt.Sprint(len(args)),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
if isUnhashable(args[0]) {
|
||||
return nil, ArErr{
|
||||
TYPE: "Runtime Error",
|
||||
message: "unhashable type: " + typeof(args[0]),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
key := ArValidToAny(args[0])
|
||||
mutex.Lock()
|
||||
m[key] = args[1]
|
||||
mutex.Unlock()
|
||||
return nil, ArErr{}
|
||||
},
|
||||
},
|
||||
"__getindex__": builtinFunc{
|
||||
"__getindex__",
|
||||
func(args ...any) (any, ArErr) {
|
||||
if len(args) != 1 {
|
||||
return nil, ArErr{
|
||||
TYPE: "TypeError",
|
||||
message: "expected 1 argument, got " + fmt.Sprint(len(args)),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
key := ArValidToAny(args[0])
|
||||
if isUnhashable(key) {
|
||||
return nil, ArErr{
|
||||
TYPE: "Runtime Error",
|
||||
message: "unhashable type: " + typeof(key),
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
mutex.RLock()
|
||||
if _, ok := m[key]; !ok {
|
||||
mutex.RUnlock()
|
||||
return nil, ArErr{
|
||||
TYPE: "KeyError",
|
||||
message: "key " + fmt.Sprint(key) + " not found",
|
||||
EXISTS: true,
|
||||
}
|
||||
}
|
||||
v := m[key]
|
||||
mutex.RUnlock()
|
||||
return v, ArErr{}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user