Giter Site home page Giter Site logo

Comments (2)

skx avatar skx commented on July 30, 2024 1

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.

csd129 avatar csd129 commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.