Comments (2)
That's an interesting idea, thanks for the suggestion.
I suspect I would not work on such a thing, but if you were willing to contribute I'd accept.
There are probably questions to be asked first though, like how this would affect scoping. The simple approach, like C using "include" would be easy to add, and could be done at the lexer/parser level:
#include "blah.monkey"
That works. But if you had an import like this:
import "time"
Where "time.monkey" had:
function today() { return "Monday"; }
Would that be called as today()
, or time.today()
? Similarly how would you handle things that were public vs. private?
And finally would you need to support some kind of search-path, as used in perl/python?
from monkey.
The code below allows for monkey files to be imported, if you want play with or certainly improve.
So if a file, say foo.monkey contains
function today() { return "Monday"; }
It will be imported with
include("foo")
with the imported file being within the env var INCLUDEPATH or in the directory /usr/local/include/monkey or current directory.
The function would be called with today()
package evaluator
import (
"os"
"fmt"
"strings"
"monkey/object"
"monkey/lexer"
"monkey/parser"
)
func Include(env *object.Environment, args ...object.Object) object.Object {
import_file:=args[0].Inspect()
includes:=""
if len(import_file) == 0 {
return newError("ArgError: include() expects a file identifier, none given")
}
imp_filename:=fmt.Sprintf("%s.monkey", import_file)
userincludes:=os.Getenv("INCLUDEPATH")
if len(userincludes) == 0 {
includes="/usr/local/include/monkey:."
} else {
includes=fmt.Sprintf("%s:%s", userincludes, "/usr/local/include/monkey:." )
}
incpaths:=strings.Split(includes, ":")
for _, ipath := range incpaths {
import_file=fmt.Sprintf("%s/%s", ipath, imp_filename)
data, err := os.ReadFile(import_file)
if err == nil {
l := lexer.New(string(data))
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) == 0 {
Eval(program, env)
return nil
}
fmt.Printf("Error parsing eval-string: %s", string(data))
os.Exit(1)
}
}
return newError("Unable to open file %s", imp_filename)
}
func init() {
RegisterBuiltin("include",
func(env *object.Environment, args ...object.Object) object.Object {
return (Include(env, args...))
})
}
from monkey.
Related Issues (20)
- Encountered a panic when attempting to retrieve an index value from a string HOT 1
- Encountered an issue when using backtick literals HOT 1
- Implement the range operator
- Crash with array.find
- Literal regular expressions should allow captures
- Implement switch-statements
- Updated embedded "standard-library" for go-implant
- call function from go HOT 1
- CI process not reporting an error.
- Ability to cancel a running Monkey Script HOT 1
- Add support for `elif` HOT 2
- Need help in adding Object Methods! HOT 5
- Asked some more stuff in #87 HOT 1
- execute env.mon got : identifier not found: self HOT 1
- iteration.mon : wrong hash key/value HOT 2
- EvalContext discards context on recursion HOT 7
- Regex Built In should avoid MustCompile to avoid potential panic
- Range operator assumes increasing direction
- Add fuzz-testing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monkey.