about summary refs log tree commit diff
path: root/.env.example
blob: 2f2fca79dc11151b4880cb619cddcc60b1e1992b (plain)
1
BOT_TOKEN="12345678910"
light .w { color: #DDD } /* Text.Whitespace */ .highlight .mb { color: #F0F } /* Literal.Number.Bin */ .highlight .mf { color: #F0F } /* Literal.Number.Float */ .highlight .mh { color: #F0F } /* Literal.Number.Hex */ .highlight .mi { color: #F0F } /* Literal.Number.Integer */ .highlight .mo { color: #F0F } /* Literal.Number.Oct */ .highlight .sa { color: #87CEEB } /* Literal.String.Affix */ .highlight .sb { color: #87CEEB } /* Literal.String.Backtick */ .highlight .sc { color: #87CEEB } /* Literal.String.Char */ .highlight .dl { color: #87CEEB } /* Literal.String.Delimiter */ .highlight .sd { color: #87CEEB } /* Literal.String.Doc */ .highlight .s2 { color: #87CEEB } /* Literal.String.Double */ .highlight .se { color: #87CEEB } /* Literal.String.Escape */ .highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */ .highlight .si { color: #87CEEB } /* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
package vm

import (
	"fmt"
	"jinx/pkg/lang/modules"
	"jinx/pkg/lang/modules/core"
	"jinx/pkg/lang/modules/natives"
	"jinx/pkg/lang/vm/value"
)

func (vm *VM) setup() error {
	vm.modules = make([]modules.Module, 0, len(vm.main.Deps()))

	// Add all natives to the VM as globals.
	for _, native := range natives.Natives {
		decollidedName := fmt.Sprintf("%s#native", native.Name)

		if _, ok := vm.globals[decollidedName]; ok {
			return fmt.Errorf("native %s already exists", decollidedName)
		}

		nativeFunc := native.Fn // Capture the native function, because Go is fun.
		wrappedFunc := func(args []value.Value) (value.Value, error) {
			return nativeFunc(vm, args)
		}

		nativeFunction := value.NewNativeFunction(wrappedFunc, uint(native.ArgCount))
		if err := vm.AddGlobal(decollidedName, nativeFunction); err != nil {
			return err
		}
	}

	for _, depRef := range vm.main.Deps() {
		dep, err := vm.resolveModule(depRef)
		if err != nil {
			return err
		}

		vm.modules = append(vm.modules, dep)
	}

	return nil
}

// TODO: Make an actual module resolver.
func (vm *VM) resolveModule(ref modules.ModuleRef) (modules.Module, error) {
	// TODO: Support other modules than core.
	switch ref.Name() {
	case "core":
		return core.Module, nil
	default:
		return modules.Module{}, fmt.Errorf("unknown module: %s", ref.Name())
	}
}