Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split stdlib into submodules #152

Closed
wants to merge 10 commits into from
Closed

Conversation

b1ek
Copy link
Member

@b1ek b1ek commented Jun 5, 2024

reworked a little bit of stdlib. the whole thing is still in an early stage, but now it can have more than one file

stating the obvious, this is a breaking change by definition.

issues:

  1. hardcoded paths. i do not know macro-fu that much to dynamically load the modules
  2. the commit history is a mess. sorry for that
  3. most of already written amber code will break. heck, it took me about 20 minutes to rewrite stdlib tests. thankfully amber is still in its early stage
    1. install script needs rewriting. working on that right now

this PR also introduces include_dir crate dependency

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jun 5, 2024

We have to update this discussion once this gets merged and released

#151

@b1ek b1ek requested review from boushley, Ph0enixKM and arapower June 8, 2024 15:05
src/compiler.rs Outdated
Comment on lines 144 to 147
std_modules.insert("".to_string(), include_str!("std/main.ab").to_string());
std_modules.insert("std".to_string(), include_str!("std/main.ab").to_string());
std_modules.insert("strings".to_string(), include_str!("std/strings.ab").to_string());
std_modules.insert("fs".to_string(), include_str!("std/fs.ab").to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any problem with this namespace definition?
I think it will affect future language design.

For example, I feel std/string is better than std/strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also struggling to understand why just the "std" isn't good enough. Is there any deep reason behind this since if we just import functions separately and they got tree-shaken, @b1ek?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also struggling to understand why just the "std" isn't good enough

as of right now, its fine as it is but when we'd want to grow it to have an imagick, curl, fs and other kind of modules it will be a mess if they are placed in one file. i have actually started working on this PR because i wanted to make a curl module but didn't want to place it in main.rs

i mean, you dont do stuff like use std::{read_file_to_string, SocketAddr, IoError} in rust, right? even if unused stuff will get tree shaken away

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Why not import everything publicly from the main.ab and have string.ab and other modules?

pub import * from "./string.ab"

Oh... this could be hard to implement for the path resolution to work. I see now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not import everything publicly from the main.ab

thats actually the right way. we probably should either load stdlib from place like /usr/lib/amber/std or look into the include_dir macro

Copy link
Member

@Ph0enixKM Ph0enixKM Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this would require the installer to fetch the stdlib as well however this would make this issue #170 impossible to implement. I think that for now we could leave the current state of the PR:

import * from "std/string"

I think that we should also discuss how the paths should work with package manager in the future. Here are some ideas I had in mind:

Not sure if we want to get our hands dirty and maintain package registry. This would require some funding and good sotware architecture as it'd be easy to abuse it. If we decide to keep things simple - we could offer importing packages directly from git repositories as the main way to fetch packages.

// Pull the code from GitHub repository (latest)
import * from "gh:b1ek/amber-library"
// Pull the code from GitHub repository with version
import * from "gh:b1ek/[email protected]"
// Pull the code from url (the version should be dependent on the url)
import * from "https://example.com/amber-library"
// Import the code from path
import * from "./path/to/file"

This would require us to implement an amber.lock file to keep the track on versioning of all dependencies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would require the installer to fetch the stdlib

why? the stdlib exists only at compile time. there's no need to source it in the script at the runtime.

as for package registry, that is probably the job for a third party cli application, as its been done for node, rust, and many other languages

Copy link
Member

@Ph0enixKM Ph0enixKM Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to point out that Deno did it in the early stage but now I see that they discourage it as people can easily put malware in the third party url.

Never mind. This was not a good idea.

I think you have a good approach here.

@Ph0enixKM Ph0enixKM added this to the Amber 0.4.0-alpha milestone Jun 17, 2024
@b1ek b1ek requested a review from arapower June 20, 2024 05:59
Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also there are merge conflicts. If you can't resolve them - let me know

src/compiler.rs Outdated
pub fn resolve_std(path: String, meta: &mut ParserMetadata, token_import: Option<Token>) -> Result<String, Failure> {
let mut std_modules = HashMap::new();

std_modules.insert("".to_string(), include_str!("std/main.ab").to_string());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this approach to let people import from:

import * from "std"
// or
import * from "std/"

As this would result in inconsistencies in path naming. I'd just error that std/ doesn't exist. What do you think?

src/compiler.rs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b1ek can you rename this module to text.ab?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ofc

@b1ek b1ek added the pr/breaking This PR introduces a breaking change label Jun 22, 2024
@Ph0enixKM Ph0enixKM linked an issue Jun 30, 2024 that may be closed by this pull request
@Mte90
Copy link
Member

Mte90 commented Jul 4, 2024

I think that this PR depends on #266

When that one will be merged we can rewrite this one and reorganize everything.

@Ph0enixKM
Copy link
Member

#266 is merged and we can now update this PR

@Mte90
Copy link
Member

Mte90 commented Jul 9, 2024

If it is fine for @b1ek I can work on a new version of this PR with the latest changes to the stdlib, as the Rust code doesn't seems so much to handle.

@b1ek
Copy link
Member Author

b1ek commented Jul 9, 2024

@Mte90 if you want to make it mergeable, you can just push commits to this branch to not do extra work

@Ph0enixKM
Copy link
Member

There is so much going on with stdlib PRs. Let's prioritize this one first

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jul 9, 2024

Okay I'm closing this PR in favour of #291

@Ph0enixKM Ph0enixKM closed this Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/breaking This PR introduces a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ should split "std" into submodules
4 participants