You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
858 B
Go
37 lines
858 B
Go
package interpreter
|
|
|
|
// environment represents the environment in which the interpreter operates.
|
|
type environment struct {
|
|
values map[string]any
|
|
}
|
|
|
|
// newEnvironment creates a new environment.
|
|
func newEnvironment() *environment {
|
|
return &environment{values: make(map[string]any)}
|
|
}
|
|
|
|
// define defines a new variable in the environment.
|
|
func (e *environment) define(name string, value any) {
|
|
e.values[name] = value
|
|
}
|
|
|
|
// get gets the value of a variable in the environment.
|
|
func (e *environment) get(name string) any {
|
|
value, ok := e.values[name]
|
|
if !ok {
|
|
panic("Undefined variable '" + name + "'.")
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
// assign assigns a new value to a variable in the environment.
|
|
func (e *environment) assign(name string, value any) {
|
|
_, ok := e.values[name]
|
|
if !ok {
|
|
panic("Undefined variable '" + name + "'.")
|
|
}
|
|
|
|
e.values[name] = value
|
|
}
|