tdenniston/bish

Importing other bish-files

egorsmkv opened this issue ยท 22 comments

Topic is relevant. You can do something like this, but it is better to determine the built-in function "eval". And then read the file and run it through this feature.

def import(f) {
    @(./bish -r $f);
}

I believe the best way to do this is to make 'import' a language feature, not a function call. I'll get to this soon.

There is now an import statement in the language. Here's an example of how to use it:

# File a.bish
def import_me() {
    println("Hello from a");
}

# File b.bish
import a;
a.import_me();

Please let me know of issues with this: it was a complicated implementation, so there are probably bugs.

@tdenniston

Such an example.

I created a copy of "a.bish" called "a2.bish" and changed "b.bish" as follows:

# File b.bish
import a;
import a2;

a.import_me("Egor");

After running the script displays this error:

bish: src/IR.cpp:49: void Bish::Module::import(Bish::Module*): Assertion `f->name.namespace_id.empty()' failed.

However, the file name is not important. Just the same function names in the files.

Also, in the future, you need to add an import scripts from folders.

@eg0r Can you verify that the bug with a and a2 is now fixed?

@tdenniston Checked. No problem.

@tdenniston Hurried.

If twice import file, displays an error:

bish: src/IR.cpp:87: void Bish::Module::import(Bish::Module*): Assertion `call->function->body == __null' failed.

File:

import a;
import a;

a.import_me();

Ok, that issue should be fixed now.

@tdenniston Yes, now works without problems.

Here's another problem: if you call the import in the file, an error will occur segmentation.

# foo.bish
import foo;
$ bish foo.bish

Noted that the examples do not work in the README. They need to be corrected.

@eg0r Thanks for catching that! Can you verify that they work now?

After compiling a file does not function (the same bish-file).

#!/usr/bin/env bash
# Autogenerated script, compiled from the Bish language.
# Bish version 0.1
# Please see https://github.com/tdenniston/bish for more information about Bish.

function main () {
    a_import_me;
}

main;

@eg0r Can you paste the bish file you are using as input?

egor@book:~/Tests/Bish > cat b.bish 
import a;
import a;

a.import_me();

egor@book:~/Tests/Bish > bish b.bish 
#!/usr/bin/env bash
# Autogenerated script, compiled from the Bish language.
# Bish version 0.1
# Please see https://github.com/tdenniston/bish for more information about Bish.

function main () {
    a_import_me;
}

main;

egor@book:~/Tests/Bish > 

I can't reproduce your output. Can you make sure you're using the latest version?

$ cat b.bish
import a;
import a;

a.import_me();
$ ./bish b.bish
#!/usr/bin/env bash
# Autogenerated script, compiled from the Bish language.
# Bish version 0.1
# Please see https://github.com/tdenniston/bish for more information about Bish.

function main () {
    a_import_me;
}

function a_import_me () {
    StdLib_println "hello from a";
}

function StdLib_println () {
    local s="$1";
    echo -e "$s";
}

main;
$

My fault, I forgot that the file has changed earlier.

Checked now works without errors.

๐Ÿ‘

For import statement,I think the error message should show the file name to indicate the location of error.

e.g.

Bish parsing error: Parsing error: Expected variable...., line 27, /path/to/userlib.bish

Hi tdenniston,

I test it with my script,and find some issues.

  • In test-userlib.bash , It generate two userlib_userlib_format_date function.And the function name have two userlib prefix.
  • In result.The expected output is 3ๆœˆ 23 12:31:45 | INFO : info message.But the actual output is 3ๆœˆ 23 12:31:45 | INFO : info.

Here is my script:

# the userlib.bish

def format_date() {
    DATEFORMAT = "%b %e %H:%M:%S";
    return @(date +"$DATEFORMAT");
}

def msg(msg) {
    date = format_date();
    @(echo "$date | $msg");
}

def msg_info(msg) {
    msg("INFO  : $msg");
}

def msg_debug(msg) {
    msg ("DEBUG : $msg");
}

# test-userlib.bish

import userlib;

userlib.msg_info("info message");
userlib.msg_debug("debug message");

# test-userlib.bash


#!/usr/bin/env bash
# Autogenerated script, compiled from the Bish language.
# Bish version 0.1
# Please see https://github.com/tdenniston/bish for more information about Bish.

function main () {
    userlib_msg_info "info message";
    userlib_msg_debug "debug message";
}

function userlib_msg_debug () {
    local msg="$1";
    userlib_userlib_msg "DEBUG : "$msg"";
}

function userlib_userlib_msg () {
    local msg="$1";
    local date=$(userlib_userlib_format_date);
    echo "$date | $msg";
}

function userlib_userlib_format_date () {
    local DATEFORMAT="%b %e %H:%M:%S";
    echo "$(date +"$DATEFORMAT")"; exit;
}

function userlib_msg_info () {
    local msg="$1";
    userlib_userlib_msg "INFO  : "$msg"";
}

function userlib_userlib_msg () {
    local msg="$1";
    local date=$(userlib_userlib_format_date);
    echo "$date | $msg";
}

function userlib_userlib_format_date () {
    local DATEFORMAT="%b %e %H:%M:%S";
    echo "$(date +"$DATEFORMAT")"; exit;
}

main;

# result
$ ./test-userlib.bash 
 3ๆœˆ 23 12:31:45 | INFO  : info
 3ๆœˆ 23 12:31:45 | DEBUG : debug

@qinshulei Thanks for reporting those. Can you verify that both problems are fixed?

@tdenniston I checked. There are no errors.

@tdenniston Great.Both problems are fixed.

I'm going to close this issue now. If any other problems come up, please report them in new issues.