Compare commits
No commits in common. "c5a843ea39b698356690ef006cbfe0ae34b42b1b" and "8d12c0bdf80ffece9c29b8687659f55bb99cf47b" have entirely different histories.
c5a843ea39
...
8d12c0bdf8
18 changed files with 100 additions and 139 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
14
.idea/anglais.iml
generated
Normal file
14
.idea/anglais.iml
generated
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true">
|
||||||
|
<buildTags>
|
||||||
|
<option name="os" value="js" />
|
||||||
|
<option name="arch" value="wasm" />
|
||||||
|
</buildTags>
|
||||||
|
</component>
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
10
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
10
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="GoDfaErrorMayBeNotNil" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<methods>
|
||||||
|
<method importPath="neemek.com/anglais/src" receiver="*Lexer" name="NextToken" />
|
||||||
|
</methods>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/anglais.iml" filepath="$PROJECT_DIR$/.idea/anglais.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -977,41 +977,6 @@ func (c *Compiler) compileAssignFromStack(to Node, sig TypeSignature, declare bo
|
||||||
}
|
}
|
||||||
|
|
||||||
return sig, nil
|
return sig, nil
|
||||||
case IndexNodeType:
|
|
||||||
if declare {
|
|
||||||
return nil, c.error(fmt.Sprintf("cannot declare an indexed item"), to)
|
|
||||||
}
|
|
||||||
|
|
||||||
n := to.(*IndexNode)
|
|
||||||
|
|
||||||
ssig, err := c.compile(n.source)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
isig, err := c.compile(n.index)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if isig.Type() != TypeInteger {
|
|
||||||
return nil, c.error(fmt.Sprintf("cannot index into list with non-integer (%s)", isig), n.index)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ssig.Type() {
|
|
||||||
case TypeList:
|
|
||||||
lsig := ssig.(*ListSignature)
|
|
||||||
if !c.typeMatches(sig, lsig.Contents) {
|
|
||||||
return nil, c.error(fmt.Sprintf("cannot assign value of type %s to list with items of type %s", sig, lsig.Contents), to)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.add(InstructionSetIndexList)
|
|
||||||
default:
|
|
||||||
return nil, c.error(fmt.Sprintf("cannot set index of %s", ssig.Type()), n.source)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sig, nil
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, c.error(fmt.Sprintf("cannot assign to %s", to.Type()), to)
|
return nil, c.error(fmt.Sprintf("cannot assign to %s", to.Type()), to)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -487,27 +487,6 @@ func quickComposite(a ...TypeSignature) TypeSignature {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func simplifyComposite(a TypeSignature) TypeSignature {
|
|
||||||
var atoms []TypeSignature
|
|
||||||
|
|
||||||
s := NewStack[TypeSignature](16)
|
|
||||||
s.Push(a)
|
|
||||||
|
|
||||||
for s.Current > 0 {
|
|
||||||
i := s.Pop()
|
|
||||||
|
|
||||||
c, ok := i.(*CompositeSignature)
|
|
||||||
if !ok {
|
|
||||||
|
|
||||||
atoms = append(atoms, i)
|
|
||||||
} else {
|
|
||||||
s.Push(c.A, c.B)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return quickComposite(atoms...)
|
|
||||||
}
|
|
||||||
|
|
||||||
type InnerSignature struct{}
|
type InnerSignature struct{}
|
||||||
|
|
||||||
func (*InnerSignature) Type() Type {
|
func (*InnerSignature) Type() Type {
|
||||||
|
|
|
||||||
|
|
@ -117,12 +117,8 @@ type Value interface {
|
||||||
// Get a member from the value. An error is returned if the member does not exist
|
// Get a member from the value. An error is returned if the member does not exist
|
||||||
Get(string) (Value, error)
|
Get(string) (Value, error)
|
||||||
|
|
||||||
// Copy create a copy of the value. A copy is a direct copy for small data (numbers) and a copy of pointer
|
// Clone create a clone of the value. The returned value is a pointer to a new value of the same type as the value.
|
||||||
// for bigger data (lists, tuples, dicts)
|
Clone() Value
|
||||||
Copy() Value
|
|
||||||
|
|
||||||
// Clone create a clone of the value. A clone is new data for all data.
|
|
||||||
//Clone() Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type NilValue struct{}
|
type NilValue struct{}
|
||||||
|
|
@ -147,7 +143,7 @@ func (v *NilValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("nil has no properties")
|
return nil, errors.New("nil has no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *NilValue) Copy() Value {
|
func (v *NilValue) Clone() Value {
|
||||||
return &NilValue{}
|
return &NilValue{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -179,7 +175,7 @@ func (v *BoolValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("booleans have no properties")
|
return nil, errors.New("booleans have no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *BoolValue) Copy() Value {
|
func (v *BoolValue) Clone() Value {
|
||||||
return &BoolValue{
|
return &BoolValue{
|
||||||
v.Boolean,
|
v.Boolean,
|
||||||
}
|
}
|
||||||
|
|
@ -262,11 +258,11 @@ func (v *ObjectValue) Get(key string) (Value, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ObjectValue) Copy() Value {
|
func (v *ObjectValue) Clone() Value {
|
||||||
m := make(map[string]Value, len(v.Members))
|
m := make(map[string]Value, len(v.Members))
|
||||||
|
|
||||||
for name, mem := range v.Members {
|
for name, mem := range v.Members {
|
||||||
m[name] = mem.Copy()
|
m[name] = mem.Clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ObjectValue{
|
return &ObjectValue{
|
||||||
|
|
@ -307,7 +303,7 @@ func (v *FloatValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("numbers have no properties")
|
return nil, errors.New("numbers have no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *FloatValue) Copy() Value {
|
func (v *FloatValue) Clone() Value {
|
||||||
return &FloatValue{
|
return &FloatValue{
|
||||||
v.Number,
|
v.Number,
|
||||||
}
|
}
|
||||||
|
|
@ -338,7 +334,7 @@ func (v *IntegerValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("numbers have no properties")
|
return nil, errors.New("numbers have no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *IntegerValue) Copy() Value {
|
func (v *IntegerValue) Clone() Value {
|
||||||
return &IntegerValue{
|
return &IntegerValue{
|
||||||
new(big.Int).Set(v.Number),
|
new(big.Int).Set(v.Number),
|
||||||
}
|
}
|
||||||
|
|
@ -431,7 +427,7 @@ func (v *StringValue) Get(key string) (Value, error) {
|
||||||
return nil, errors.New(fmt.Sprintf("string has no property \"%s\"", key))
|
return nil, errors.New(fmt.Sprintf("string has no property \"%s\"", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *StringValue) Copy() Value {
|
func (v *StringValue) Clone() Value {
|
||||||
return &StringValue{
|
return &StringValue{
|
||||||
v.Text,
|
v.Text,
|
||||||
}
|
}
|
||||||
|
|
@ -598,9 +594,15 @@ func (v *ListValue) Get(key string) (Value, error) {
|
||||||
return nil, errors.New(fmt.Sprintf("list has no property \"%s\"", key))
|
return nil, errors.New(fmt.Sprintf("list has no property \"%s\"", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ListValue) Copy() Value {
|
func (v *ListValue) Clone() Value {
|
||||||
|
n := make([]Value, len(v.Items))
|
||||||
|
|
||||||
|
for i, item := range v.Items {
|
||||||
|
n[i] = item.Clone()
|
||||||
|
}
|
||||||
|
|
||||||
return &ListValue{
|
return &ListValue{
|
||||||
v.Items,
|
n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -633,9 +635,13 @@ func (v *TupleValue) DebugString() string {
|
||||||
return v.String()
|
return v.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *TupleValue) Copy() Value {
|
func (v *TupleValue) Clone() Value {
|
||||||
|
n := make([]Value, len(v.Items))
|
||||||
|
for i, item := range v.Items {
|
||||||
|
n[i] = item.Clone()
|
||||||
|
}
|
||||||
return &TupleValue{
|
return &TupleValue{
|
||||||
v.Items,
|
n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -712,7 +718,7 @@ func (v *FunctionValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("functions have no properties")
|
return nil, errors.New("functions have no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *FunctionValue) Copy() Value {
|
func (v *FunctionValue) Clone() Value {
|
||||||
return &FunctionValue{
|
return &FunctionValue{
|
||||||
v.Name,
|
v.Name,
|
||||||
v.Params,
|
v.Params,
|
||||||
|
|
@ -752,7 +758,7 @@ func (v *BuiltinFunctionValue) Get(_ string) (Value, error) {
|
||||||
return nil, errors.New("functions have no properties")
|
return nil, errors.New("functions have no properties")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *BuiltinFunctionValue) Copy() Value {
|
func (v *BuiltinFunctionValue) Clone() Value {
|
||||||
return &BuiltinFunctionValue{
|
return &BuiltinFunctionValue{
|
||||||
v.Name,
|
v.Name,
|
||||||
v.Signature,
|
v.Signature,
|
||||||
|
|
@ -801,7 +807,7 @@ func (v *RecordValue) DebugString() string {
|
||||||
return v.String()
|
return v.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *RecordValue) Copy() Value {
|
func (v *RecordValue) Clone() Value {
|
||||||
return &RecordValue{
|
return &RecordValue{
|
||||||
v.Entries,
|
v.Entries,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
34
core/vm.go
34
core/vm.go
|
|
@ -152,14 +152,10 @@ const (
|
||||||
// is the index. [..., container, index] -> [..., item]
|
// is the index. [..., container, index] -> [..., item]
|
||||||
InstructionIndexTuple
|
InstructionIndexTuple
|
||||||
// InstructionIndexString index into a string. The lower item is the container, and the top item
|
// InstructionIndexString index into a string. The lower item is the container, and the top item
|
||||||
// is the index. [..., container, index] -> [..., item]. Produces a new string with only the character
|
// is the index. [..., container, index] -> [..., item]. Produces a new string with the character
|
||||||
// at the indexed position
|
// at the position
|
||||||
InstructionIndexString
|
InstructionIndexString
|
||||||
|
|
||||||
// InstructionSetIndexList set the item at a given index in a list.
|
|
||||||
// [..., item, container, index] -> [..., item]
|
|
||||||
InstructionSetIndexList
|
|
||||||
|
|
||||||
// InstructionBreakpoint for debugging purposes
|
// InstructionBreakpoint for debugging purposes
|
||||||
InstructionBreakpoint
|
InstructionBreakpoint
|
||||||
)
|
)
|
||||||
|
|
@ -642,7 +638,7 @@ var DefaultGlobals = map[string]Value{
|
||||||
n, _ := v.Number.Float64()
|
n, _ := v.Number.Float64()
|
||||||
return &FloatValue{n}, nil
|
return &FloatValue{n}, nil
|
||||||
case *FloatValue:
|
case *FloatValue:
|
||||||
return v.Copy(), nil
|
return v.Clone(), nil
|
||||||
case *StringValue:
|
case *StringValue:
|
||||||
num, err := strconv.ParseFloat(v.Text, FloatSize)
|
num, err := strconv.ParseFloat(v.Text, FloatSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -982,7 +978,7 @@ func (vm *VM) Next() bool {
|
||||||
vm.Stack.Push(v)
|
vm.Stack.Push(v)
|
||||||
|
|
||||||
case InstructionSetLocal:
|
case InstructionSetLocal:
|
||||||
value := vm.Stack.Peek().Copy()
|
value := vm.Stack.Peek().Clone()
|
||||||
name := vm.GetConstant(vm.NextByte()).(*StringValue).Text
|
name := vm.GetConstant(vm.NextByte()).(*StringValue).Text
|
||||||
|
|
||||||
vm.setVar(name, value)
|
vm.setVar(name, value)
|
||||||
|
|
@ -990,7 +986,7 @@ func (vm *VM) Next() bool {
|
||||||
case InstructionDeclareLocal:
|
case InstructionDeclareLocal:
|
||||||
vm.addVar(
|
vm.addVar(
|
||||||
vm.GetConstant(vm.NextByte()).(*StringValue).Text,
|
vm.GetConstant(vm.NextByte()).(*StringValue).Text,
|
||||||
vm.Stack.Peek().Copy(),
|
vm.Stack.Peek().Clone(),
|
||||||
)
|
)
|
||||||
|
|
||||||
case InstructionGetGlobal:
|
case InstructionGetGlobal:
|
||||||
|
|
@ -1074,7 +1070,7 @@ func (vm *VM) Next() bool {
|
||||||
vm.Stack.Push(r, l)
|
vm.Stack.Push(r, l)
|
||||||
|
|
||||||
case InstructionDuplicate:
|
case InstructionDuplicate:
|
||||||
vm.Stack.Push(vm.Stack.Peek().Copy())
|
vm.Stack.Push(vm.Stack.Peek().Clone())
|
||||||
|
|
||||||
case InstructionAccessProperty:
|
case InstructionAccessProperty:
|
||||||
source := vm.Stack.Pop()
|
source := vm.Stack.Pop()
|
||||||
|
|
@ -1116,7 +1112,7 @@ func (vm *VM) Next() bool {
|
||||||
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
||||||
}
|
}
|
||||||
|
|
||||||
vm.Stack.Push(l.Items[n].Copy())
|
vm.Stack.Push(l.Items[n].Clone())
|
||||||
|
|
||||||
case InstructionIndexTuple:
|
case InstructionIndexTuple:
|
||||||
i := vm.Stack.Pop().(*IntegerValue)
|
i := vm.Stack.Pop().(*IntegerValue)
|
||||||
|
|
@ -1128,7 +1124,7 @@ func (vm *VM) Next() bool {
|
||||||
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
vm.error(fmt.Sprintf("index %d out of bounds", n))
|
||||||
}
|
}
|
||||||
|
|
||||||
vm.Stack.Push(t.Items[n].Copy())
|
vm.Stack.Push(t.Items[n].Clone())
|
||||||
|
|
||||||
case InstructionIndexString:
|
case InstructionIndexString:
|
||||||
i := vm.Stack.Pop().(*IntegerValue)
|
i := vm.Stack.Pop().(*IntegerValue)
|
||||||
|
|
@ -1142,18 +1138,6 @@ func (vm *VM) Next() bool {
|
||||||
|
|
||||||
vm.Stack.Push(&StringValue{string(s.Text[n])})
|
vm.Stack.Push(&StringValue{string(s.Text[n])})
|
||||||
|
|
||||||
case InstructionSetIndexList:
|
|
||||||
n := vm.Stack.Pop().(*IntegerValue)
|
|
||||||
l := vm.Stack.Pop().(*ListValue)
|
|
||||||
|
|
||||||
i := n.Number.Int64()
|
|
||||||
|
|
||||||
if i < 0 || int64(len(l.Items)) <= i {
|
|
||||||
vm.error(fmt.Sprintf("index %d out of bounds", i))
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Items[i] = vm.Stack.Peek().Copy()
|
|
||||||
|
|
||||||
case InstructionBreakpoint:
|
case InstructionBreakpoint:
|
||||||
/*
|
/*
|
||||||
// I'm keeping this
|
// I'm keeping this
|
||||||
|
|
@ -1300,7 +1284,7 @@ func (vm *VM) HasNext() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vm *VM) GetConstant(id Bytecode) Value {
|
func (vm *VM) GetConstant(id Bytecode) Value {
|
||||||
return vm.chunk.Constants[id].Copy()
|
return vm.chunk.Constants[id].Clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vm *VM) ReadConstant() Value {
|
func (vm *VM) ReadConstant() Value {
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,14 @@
|
||||||
|
|
||||||
fn range(from: int, to: int) -> (fn() -> (int, bool)) {
|
fn range(from: int, to: int) -> (fn() -> (int, bool)) {
|
||||||
i := from - 1
|
i := from - 1
|
||||||
|
end := to - 1
|
||||||
|
|
||||||
fn() -> (int, bool) {
|
fn() -> (int, bool) {
|
||||||
(i = i+1, i+1 < to)
|
if i < end {
|
||||||
|
(i = i+1, true)
|
||||||
|
} else {
|
||||||
|
(-1, false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
# Empty list
|
# Empty list
|
||||||
println([]any)
|
println([])
|
||||||
|
|
||||||
# List with items
|
# List with items
|
||||||
println([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
println([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
||||||
|
|
@ -8,25 +8,25 @@ println([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
||||||
# List with items of different types
|
# List with items of different types
|
||||||
println(["", "私はかっこいいです。", true, nil, nil, 1, 2])
|
println(["", "私はかっこいいです。", true, nil, nil, 1, 2])
|
||||||
|
|
||||||
a := []int
|
a := []
|
||||||
|
|
||||||
a.push(1)
|
a = a + [1]
|
||||||
a.push(2)
|
a = a + [2]
|
||||||
|
|
||||||
println(a)
|
println(a)
|
||||||
|
|
||||||
|
|
||||||
list := []int
|
list := []
|
||||||
|
|
||||||
x := 0
|
x := 0
|
||||||
for n in 0..100 {
|
for n in 0..100 {
|
||||||
x = x + 2*n + 1
|
x = x + 2*n + 1
|
||||||
|
|
||||||
list.push(x)
|
list = list + [x]
|
||||||
}
|
}
|
||||||
|
|
||||||
println(list)
|
println(list)
|
||||||
println(list.map(fn(a: int) -> int {
|
println(list.map(func(a) {
|
||||||
return a - 1
|
return a - 1
|
||||||
}))
|
}))
|
||||||
println(list.length())
|
println(list.length())
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,14 @@
|
||||||
# This program computes the fibonacci numbers using recursion (O(2^n))
|
# This program computes the fibonacci numbers using recursion (O(2^n))
|
||||||
# It is very slow
|
# It is very slow
|
||||||
fn fib(x: number) number {
|
func fib(x: number) number {
|
||||||
if x <= 1 {
|
if x <= 1 {
|
||||||
x
|
return x
|
||||||
} else {
|
|
||||||
fib(x - 1) + fib(x - 2)
|
|
||||||
}
|
}
|
||||||
|
return fib(x - 1) + fib(x - 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
n := 0
|
n := 0
|
||||||
while n < 100 {
|
while n < 100 {
|
||||||
println(fib(n))
|
write(str(fib(n)))
|
||||||
n = n + 1
|
n = n + 1
|
||||||
}
|
}
|
||||||
|
|
|
||||||
0
foo.ang
0
foo.ang
|
|
@ -1 +0,0 @@
|
||||||
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
fn map<T, R>(list: [T], f: fn(T) -> R) -> [R] {
|
fn<T, R> ([T]) map(f: fn(T) -> R) -> [R] {
|
||||||
out := []
|
out := []
|
||||||
|
|
||||||
for v in list.iter() {
|
for v in list.iter() {
|
||||||
|
|
|
||||||
|
|
@ -249,5 +249,3 @@ fn tan(x: float) -> float {
|
||||||
# todo
|
# todo
|
||||||
0.0
|
0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
(E:, PI:, sqrt:, log:, ln:, exp:, pow:)
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,25 @@
|
||||||
|
|
||||||
NAMESPACE := ""
|
NAMESPACE := ""
|
||||||
|
|
||||||
fn namespace(name: str, test: fn()) {
|
func namespace(name: string, test: func()) {
|
||||||
NAMESPACE = name
|
NAMESPACE = name
|
||||||
test()
|
test()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq<T>(a: T, b: T) {
|
func assertEqual(a: any, b: any) {
|
||||||
if a != b {
|
if a != b {
|
||||||
println(format("assertion error: % should (but doesn't) equal %", [a, b]))
|
write(format("assertion error: % should (but doesn't) equal %", [a, b]))
|
||||||
exit(1)
|
exit(1)
|
||||||
} else if env("DEBUG") != "" {
|
} else if env("DEBUG") != "" {
|
||||||
println(format("assertion success: % equals %", [a, b]))
|
write(format("assertion success: % equals %", [a, b]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn neq<T>(a: T, b: T) {
|
func assertNotEqual(a: any, b: any) {
|
||||||
if a == b {
|
if a == b {
|
||||||
println(format("assertion error: % shouldn't (but does) equal %", [a, b]))
|
write(format("assertion error: % shouldn't (but does) equal %", [a, b]))
|
||||||
exit(1)
|
exit(1)
|
||||||
} else if env("DEBUG") != "" {
|
} else if env("DEBUG") != "" {
|
||||||
println(format("assertion success: % doesn't equal %", [a, b]))
|
write(format("assertion success: % doesn't equal %", [a, b]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,3 @@ assertEq("the, first, time".split(", "), ["the", "first", "time"])
|
||||||
# list indexing
|
# list indexing
|
||||||
assertEq([1, 2, 3].at(1), 2)
|
assertEq([1, 2, 3].at(1), 2)
|
||||||
assertEq(["a", "b", "c"].at(2), "c")
|
assertEq(["a", "b", "c"].at(2), "c")
|
||||||
|
|
||||||
# mutating list
|
|
||||||
a := [1, 2, 3]
|
|
||||||
assertEq(a, [1, 2, 3])
|
|
||||||
|
|
||||||
a[1] = 4
|
|
||||||
assertEq(a, [1, 4, 3])
|
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,3 @@ fn neighbours(n: int) -> (int, int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEq(neighbours(2), (1, 3))
|
assertEq(neighbours(2), (1, 3))
|
||||||
|
|
||||||
a := (1, 2)
|
|
||||||
assertEq(a, (1, 2))
|
|
||||||
|
|
||||||
(x, y) := a
|
|
||||||
assertEq(x, 1)
|
|
||||||
assertEq(y, 2)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue