add colour object

This commit is contained in:
2023-06-19 23:58:34 +01:00
parent 3f048ce645
commit 3bf48079d5
9 changed files with 370 additions and 42 deletions

View File

@@ -13,7 +13,18 @@ func anyToBool(x any) bool {
case nil: case nil:
return false return false
case ArObject: case ArObject:
return anyToBool(ArValidToAny(x)) if y, ok := x.obj["__Boolean__"]; ok {
val, err := runCall(
call{
callable: y,
args: []any{},
}, stack{}, 0)
if err.EXISTS {
return false
}
return anyToBool(val)
}
return false
case builtinFunc: case builtinFunc:
return true return true
case Callable: case Callable:

View File

@@ -150,8 +150,35 @@ func makeGlobal() ArObject {
vars["cot"] = ArCot vars["cot"] = ArCot
vars["arccot"] = ArArccot vars["arccot"] = ArArccot
vars["todeg"] = ArToDeg vars["todeg"] = ArToDeg
vars["colour"] = ArColour
vars["torad"] = ArToRad vars["torad"] = ArToRad
vars["abs"] = ArAbs vars["abs"] = ArAbs
vars["fraction"] = builtinFunc{"fraction", func(a ...any) (any, ArErr) {
if len(a) == 0 {
return nil, ArErr{TYPE: "fraction", message: "fraction takes 1 argument",
EXISTS: true}
}
switch x := a[0].(type) {
case number:
return ArString(x.String()), ArErr{}
case ArObject:
if callable, ok := x.obj["__fraction__"]; ok {
resp, err := runCall(
call{
callable: callable,
args: []any{},
},
stack{},
0,
)
if err.EXISTS {
return nil, err
}
return resp, ArErr{}
}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot fraction '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["dir"] = builtinFunc{"dir", func(a ...any) (any, ArErr) { vars["dir"] = builtinFunc{"dir", func(a ...any) (any, ArErr) {
if len(a) == 0 { if len(a) == 0 {
return ArArray([]any{}), ArErr{} return ArArray([]any{}), ArErr{}

96
src/colour.go Normal file
View File

@@ -0,0 +1,96 @@
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/jwalton/go-supportscolor"
)
var ArColour = Map(
anymap{
"set": builtinFunc{"set", func(a ...any) (any, ArErr) {
if len(a) != 2 {
return nil, ArErr{
TYPE: "TypeError",
message: "set() takes exactly 2 argument (" + fmt.Sprint(len(a)) + " given)",
EXISTS: true,
}
}
var c *color.Color
var s string
if x, ok := a[0].(number); ok {
c = color.Set(color.Attribute(x.Num().Int64()))
} else {
return nil, ArErr{
TYPE: "TypeError",
message: "set() argument 1 must be an number, not " + typeof(a[0]),
EXISTS: true,
}
}
if typeof(a[1]) == "string" {
s = ArValidToAny(a[1]).(string)
} else {
return nil, ArErr{
TYPE: "TypeError",
message: "set() argument 2 must be a string, not " + typeof(a[1]),
EXISTS: true,
}
}
if supportscolor.Stdout().SupportsColor {
return c.Sprint(s), ArErr{}
} else {
return s, ArErr{}
}
}},
"bg": Map(
anymap{
"black": newNumber().SetInt64(int64(color.BgBlack)),
"red": newNumber().SetInt64(int64(color.BgRed)),
"green": newNumber().SetInt64(int64(color.BgGreen)),
"yellow": newNumber().SetInt64(int64(color.BgYellow)),
"blue": newNumber().SetInt64(int64(color.BgBlue)),
"magenta": newNumber().SetInt64(int64(color.BgMagenta)),
"cyan": newNumber().SetInt64(int64(color.BgCyan)),
"white": newNumber().SetInt64(int64(color.BgWhite)),
"hiBlack": newNumber().SetInt64(int64(color.BgHiBlack)),
"hiRed": newNumber().SetInt64(int64(color.BgHiRed)),
"hiGreen": newNumber().SetInt64(int64(color.BgHiGreen)),
"hiYellow": newNumber().SetInt64(int64(color.BgHiYellow)),
"hiBlue": newNumber().SetInt64(int64(color.BgHiBlue)),
"hiMagenta": newNumber().SetInt64(int64(color.BgHiMagenta)),
"hiCyan": newNumber().SetInt64(int64(color.BgHiCyan)),
"hiWhite": newNumber().SetInt64(int64(color.BgHiWhite)),
},
),
"fg": Map(
anymap{
"black": newNumber().SetInt64(int64(color.FgBlack)),
"red": newNumber().SetInt64(int64(color.FgRed)),
"green": newNumber().SetInt64(int64(color.FgGreen)),
"yellow": newNumber().SetInt64(int64(color.FgYellow)),
"blue": newNumber().SetInt64(int64(color.FgBlue)),
"magenta": newNumber().SetInt64(int64(color.FgMagenta)),
"cyan": newNumber().SetInt64(int64(color.FgCyan)),
"white": newNumber().SetInt64(int64(color.FgWhite)),
"hiBlack": newNumber().SetInt64(int64(color.FgHiBlack)),
"hiRed": newNumber().SetInt64(int64(color.FgHiRed)),
"hiGreen": newNumber().SetInt64(int64(color.FgHiGreen)),
"hiYellow": newNumber().SetInt64(int64(color.FgHiYellow)),
"hiBlue": newNumber().SetInt64(int64(color.FgHiBlue)),
"hiMagenta": newNumber().SetInt64(int64(color.FgHiMagenta)),
"hiCyan": newNumber().SetInt64(int64(color.FgHiCyan)),
"hiWhite": newNumber().SetInt64(int64(color.FgHiWhite)),
},
),
"reset": newNumber().SetInt64(int64(color.Reset)),
"bold": newNumber().SetInt64(int64(color.Bold)),
"faint": newNumber().SetInt64(int64(color.Faint)),
"italic": newNumber().SetInt64(int64(color.Italic)),
"underline": newNumber().SetInt64(int64(color.Underline)),
"blinkSlow": newNumber().SetInt64(int64(color.BlinkSlow)),
"blinkRapid": newNumber().SetInt64(int64(color.BlinkRapid)),
"reverseVideo": newNumber().SetInt64(int64(color.ReverseVideo)),
"concealed": newNumber().SetInt64(int64(color.Concealed)),
"crossedOut": newNumber().SetInt64(int64(color.CrossedOut)),
})

1
src/infinity.go Normal file
View File

@@ -0,0 +1 @@
package main

View File

@@ -6,7 +6,7 @@ import (
"sync" "sync"
) )
var mapCompiled = makeRegex(`( )*<( |\n)*(((.|\n)+)(,(.|\n)+)*)?( |\n)*>( )*`) var mapCompiled = makeRegex(`( )*{( |\n)*(((.|\n)+)(,(.|\n)+)*)?( |\n)*}( )*`)
type createMap struct { type createMap struct {
body anymap body anymap
@@ -20,7 +20,7 @@ func isMap(code UNPARSEcode) bool {
} }
func parseMap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr, int) { func parseMap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr, int) {
trimmed := strings.Trim(code.code, " ") trimmed := strings.TrimSpace(code.code)
trimmed = trimmed[1 : len(trimmed)-1] trimmed = trimmed[1 : len(trimmed)-1]
debugPrintln(trimmed) debugPrintln(trimmed)
return Map(anymap{}), true, ArErr{}, 1 return Map(anymap{}), true, ArErr{}, 1

View File

@@ -146,6 +146,21 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return anyToBool(val), ArErr{}
}
}
}
if x, ok := resp2.(ArObject); ok {
if y, ok := x.obj["__GreaterThanEqual__"]; ok {
val, err := runCall(
call{
y,
[]any{resp},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
@@ -173,6 +188,21 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return anyToBool(val), ArErr{}
}
}
}
if x, ok := resp2.(ArObject); ok {
if y, ok := x.obj["__LessThanEqual__"]; ok {
val, err := runCall(
call{
y,
[]any{resp},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
@@ -200,12 +230,27 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return anyToBool(val), ArErr{}
}
}
if x, ok := resp2.(ArObject); ok {
if y, ok := x.obj["__GreaterThan__"]; ok {
val, err := runCall(
call{
y,
[]any{resp},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
return anyToBool(val), ArErr{} return anyToBool(val), ArErr{}
} }
} }
}
return false, ArErr{ return false, ArErr{
"Runtime Error", "Runtime Error",
"Cannot compare type '" + typeof(resp) + "' with type '" + typeof(resp2) + "' with opperation '<'", "Cannot compare type '" + typeof(resp) + "' with type '" + typeof(resp2) + "' with opperation '<'",
@@ -227,6 +272,21 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return anyToBool(val), ArErr{}
}
}
}
if x, ok := resp2.(ArObject); ok {
if y, ok := x.obj["__LessThan__"]; ok {
val, err := runCall(
call{
y,
[]any{resp},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
@@ -290,6 +350,21 @@ func calcNegative(o operationType, stack stack, stacklevel int) (any, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return val, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostSubtract__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
@@ -342,11 +417,26 @@ func calcDivide(o operationType, stack stack, stacklevel int) (any, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return val, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostDivide__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
output = val return val, ArErr{}
return output, ArErr{}
} }
} }
return nil, ArErr{ return nil, ArErr{
@@ -462,11 +552,26 @@ func calcMul(o operationType, stack stack, stacklevel int) (any, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
output = val
return output, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostMultiply__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
output = val return val, ArErr{}
return output, ArErr{}
} }
} }
return nil, ArErr{ return nil, ArErr{
@@ -511,7 +616,6 @@ func calcOr(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack, stack,
stacklevel+1, stacklevel+1,
) )
resp = ArValidToAny(resp)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
@@ -523,7 +627,6 @@ func calcOr(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack, stack,
stacklevel+1, stacklevel+1,
) )
resp = ArValidToAny(resp)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
@@ -644,10 +747,25 @@ func notequals(a any, b any, o operationType, stack stack, stacklevel int) (bool
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return !anyToBool(val), ArErr{}
}
}
}
if x, ok := b.(ArObject); ok {
if y, ok := x.obj["__NotEqual__"]; ok {
val, err := runCall(
call{
y,
[]any{a},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
return !anyToBool(val), ArErr{} return anyToBool(val), ArErr{}
} }
} }
return !reflect.DeepEqual(a, b), ArErr{} return !reflect.DeepEqual(a, b), ArErr{}
@@ -667,6 +785,21 @@ func equals(a any, b any, o operationType, stack stack, stacklevel int) (bool, A
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
return anyToBool(val), ArErr{}
}
}
}
if x, ok := b.(ArObject); ok {
if y, ok := x.obj["__GreaterThanEqual__"]; ok {
val, err := runCall(
call{
y,
[]any{a},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
@@ -715,11 +848,27 @@ func calcMod(o operationType, stack stack, stacklevel int) (any, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
output = val
return output, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostModulo__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
output = val return val, ArErr{}
return output, ArErr{}
} }
} }
return nil, ArErr{ return nil, ArErr{
@@ -767,11 +916,26 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
o.line, o.line,
o.path, o.path,
}, stack, stacklevel+1) }, stack, stacklevel+1)
if !err.EXISTS {
output = val
return output, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostIntDivide__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
output = val return val, ArErr{}
return output, ArErr{}
} }
} }
return nil, ArErr{ return nil, ArErr{
@@ -784,7 +948,7 @@ func calcIntDiv(o operationType, stack stack, stacklevel int) (any, ArErr) {
} }
} }
func calcPower(o operationType, stack stack, stacklevel int) (number, ArErr) { func calcPower(o operationType, stack stack, stacklevel int) (any, ArErr) {
resp, err := runVal( resp, err := runVal(
o.value1, o.value1,
stack, stack,
@@ -849,16 +1013,39 @@ func calcPower(o operationType, stack stack, stacklevel int) (number, ArErr) {
} }
output.Mul(output, calculated) output.Mul(output, calculated)
} }
return output, ArErr{}
/* } else if x, ok := resp.(ArObject); ok {
n1, _ := output.Float64() if y, ok := x.obj["__Power__"]; ok {
n2, _ := resp.(number).Float64() val, err := runCall(
output = newNumber().SetFloat64(math.Pow(n1, n2)) call{
if output == nil { y,
output = infinity []any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if !err.EXISTS {
return val, ArErr{}
}
}
}
if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__PostPower__"]; ok {
val, err := runCall(
call{
y,
[]any{output},
o.code,
o.line,
o.path,
}, stack, stacklevel+1)
if err.EXISTS {
return nil, err
}
return val, ArErr{}
}
} }
*/
} else {
return nil, ArErr{ return nil, ArErr{
"Runtime Error", "Runtime Error",
"Cannot calculate power of type '" + typeof(resp) + "'", "Cannot calculate power of type '" + typeof(resp) + "'",
@@ -868,8 +1055,6 @@ func calcPower(o operationType, stack stack, stacklevel int) (number, ArErr) {
true, true,
} }
} }
return output, ArErr{}
}
func runOperation(o operationType, stack stack, stacklevel int) (any, ArErr) { func runOperation(o operationType, stack stack, stacklevel int) (any, ArErr) {
switch o.operation { switch o.operation {

View File

@@ -74,9 +74,11 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored
if colored { if colored {
output = append(output, "\x1b[0m") output = append(output, "\x1b[0m")
} }
case ArObject:
case anymap: case anymap:
if len(x) == 0 { if len(x) == 0 {
return "<>" return "{}"
} }
keys := make([]any, len(x)) keys := make([]any, len(x))
sort.Slice(keys, func(i, j int) bool { sort.Slice(keys, func(i, j int) bool {
@@ -107,7 +109,7 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored
} }
output = append(output, keyval+": "+anyToArgon(x[key], true, true, depth-1, indent+1, colored, plain)) output = append(output, keyval+": "+anyToArgon(x[key], true, true, depth-1, indent+1, colored, plain))
} }
return "<" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + ">" return "{" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + "}"
case []any: case []any:
singleline := len(x) <= 3 singleline := len(x) <= 3
output := []string{} output := []string{}

View File

@@ -91,9 +91,16 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
} else if isString(code) { } else if isString(code) {
return parseString(code) return parseString(code)
} else if issquareroot(code) { } else if issquareroot(code) {
return parseSquareroot(code, index, codelines) resp, worked, err, i = parseSquareroot(code, index, codelines)
} else if isFactorial(code) { if worked {
return parseFactorial(code, index, codelines) return resp, worked, err, i
}
}
if isFactorial(code) {
resp, worked, err, i = parseFactorial(code, index, codelines)
if worked {
return resp, worked, err, i
}
} }
if isVariable(code) { if isVariable(code) {
return parseVariable(code) return parseVariable(code)

View File

@@ -275,10 +275,9 @@ func setVariableValue(v setVariable, stack stack, stacklevel int) (any, ArErr) {
case ArObject: case ArObject:
if _, ok := y.obj["__setindex__"]; ok { if _, ok := y.obj["__setindex__"]; ok {
callable := y.obj["__setindex__"] callable := y.obj["__setindex__"]
r := ArValidToAny(resp)
_, err := runCall(call{ _, err := runCall(call{
callable: callable, callable: callable,
args: []any{key, r}, args: []any{key, resp},
line: v.line, line: v.line,
path: v.path, path: v.path,
code: v.code, code: v.code,