magpie icon indicating copy to clipboard operation
magpie copied to clipboard

idea: add namespace management

Open ghost opened this issue 5 years ago • 6 comments

@haifenghuang

You can add something like this:

from module include *

function() // instead of module.function()

The magpie is amazing! Thanks!

ghost avatar Aug 14 '20 21:08 ghost

For the moment, I have no plan of adding this. Maybe someday I'll add this support.

haifenghuang avatar Aug 16 '20 09:08 haifenghuang

@haifenghuang why I can’t include magpie modules?

include os // returns an error module not found
include net // too

Are they included by default? And is there any way to do not include them by default?

ghost avatar Aug 17 '20 21:08 ghost

You don't need to include os or net because they are built-in modules. You just need to use them, Take os built-in module for an example:

println(os.args())
println(os.getenv("PATH"))
println(os.getwd())
println(os.PATH_SEPARATOR)

Which methods or constants the os module contain? You can take a look at os.go source file. Particularly SetGlobalObj and CallMethod.

BTW, there is no way to not include them by default, sorry for this.

But..., If you really do not want to include them by default, then you must modify the source. How? In object.go, there is a init method, which registers all the built-in modules. You can comment the modules you don't want. for example, you don't want the os module, then you could comment NewOsObj() line.

haifenghuang avatar Aug 18 '20 02:08 haifenghuang

@haifenghuang I want to add ability to include this modules from:

func (p *Parser) getIncludedStatements(importpath string) (*ast.Program, error) { ... }

Can I add NewOsObj() to parser.go to getIncludedStatements. Is a NewOsObj() the part of object.go and can I import this function to parser.go?

Thanks

ghost avatar Aug 18 '20 09:08 ghost

I think you can not do it.

If you check the os.go or object.go, You can see that they are all dealing with object, not AST(Abstract Syntax Tree).

What I mean is that in parser phase, it deals with AST, but in evaluator phase, it mainly deals with object. They are different phases of the processing.

haifenghuang avatar Aug 18 '20 09:08 haifenghuang

For your understanding, the magpie interpreter works like below:

   Lexer---->Parser---->Evaluator

Below table lists the input & output of each phase:

Input Phase Output
source Lexer token
token Parser AST
AST Evaluator object

haifenghuang avatar Aug 18 '20 10:08 haifenghuang