Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed extensions/vscode/modu-lang-0.0.1.vsix
Binary file not shown.
1 change: 1 addition & 0 deletions lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["programming-language", "cli", "interpreter", "language"]
[dependencies]
bat = "0.24.0"
chrono = "0.4.39"
libloading = "0.8.6"
logos = "0.15.0"
rand = "0.8.5"
reqwest = { version = "0.12.11", features = ["blocking", "json"] }
Expand Down
10 changes: 10 additions & 0 deletions lang/examples/advanced/ffi_http/http_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "http_client"
version = "0.1.0"
edition = "2024"

[dependencies]
reqwest = { version = "0.12.11", features = ["blocking"] }

[lib]
crate-type = ["cdylib"]
26 changes: 26 additions & 0 deletions lang/examples/advanced/ffi_http/http_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use reqwest::blocking::Client;

#[unsafe(no_mangle)]
pub extern "C" fn get(
argc: std::ffi::c_int,
argv: *const *const std::ffi::c_char
) -> *mut std::ffi::c_char {
if argc != 1 {
panic!("function 'get' requires and takes 1 arguments");
}

let args = unsafe {
std::slice::from_raw_parts(argv, argc as usize)
};

let url = unsafe {
std::ffi::CStr::from_ptr(args[0])
};
let url = url.to_str().unwrap();

let client = Client::new();
let res = client.get(url).send().unwrap();
let body = res.text().unwrap();

std::ffi::CString::new(body).unwrap().into_raw()
}
Binary file added lang/examples/advanced/ffi_http/libhttp_client.so
Binary file not shown.
28 changes: 28 additions & 0 deletions lang/examples/advanced/ffi_http/main.modu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "os" as os;
import "ffi" as ffi;

fn check_comp() {
if os.name != "linux" {
print("libhttp only supports linux at the current moment");
exit();
}
}

fn get(url) {
let res = ffi.call("./libhttp_client.so", "get", url);

return res;
}

check_comp();
print(get("https://jsonplaceholder.typicode.com/todos/1"));

// Expected output:
/*
{
userId: 1,
id: 1,
title: delectus aut autem,
completed: false
}
*/
9 changes: 9 additions & 0 deletions lang/examples/ffi/ffi_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ffi_test"
version = "0.1.0"
edition = "2024"

[dependencies]

[lib]
crate-type = ["cdylib"]
109 changes: 109 additions & 0 deletions lang/examples/ffi/ffi_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#[unsafe(no_mangle)]
pub extern "C" fn add(
argc: std::ffi::c_int,
argv: *const *const std::ffi::c_char
) -> i32 {
if argc != 2 {
panic!("add requires 2 arguments");
}

let args = unsafe {
std::slice::from_raw_parts(argv, argc as usize)
};

// modu ffi cant have numbers as args cause shit broke
let num1 = unsafe {
std::ffi::CString::from_raw(args[0] as *mut std::ffi::c_char)
};
let num1 = num1.to_str().unwrap().parse::<i32>().unwrap();

let num2 = unsafe {
std::ffi::CString::from_raw(args[1] as *mut std::ffi::c_char)
};
let num2 = num2.to_str().unwrap().parse::<i32>().unwrap();

num1 + num2
}

#[unsafe(no_mangle)]
pub extern "C" fn a(
argc: std::ffi::c_int,
argv: *const *const std::ffi::c_char
) -> i32 {
if argc != 1 {
panic!("a requires 1 argument");
}

let args = unsafe {
std::slice::from_raw_parts(argv, argc as usize)
};

let a = unsafe {
std::ffi::CString::from_raw(args[0] as *mut std::ffi::c_char)
};
let a = a.to_str().unwrap();

a.parse().unwrap()
}

#[unsafe(no_mangle)]
pub extern "C" fn one() -> i64 {
1
}

#[unsafe(no_mangle)]
pub extern "C" fn string() -> *mut std::ffi::c_char {
std::ffi::CString::new("Hello, World!").unwrap().into_raw()
}

#[unsafe(no_mangle)]
pub extern "C" fn print(
argc: std::ffi::c_int,
argv: *const *const std::ffi::c_char
) {
if argc != 1 {
panic!("print requires 1 argument");
}

let args = unsafe {
std::slice::from_raw_parts(argv, argc as usize)
};

let str = unsafe {
std::ffi::CStr::from_ptr(args[0])
};

println!("{}", str.to_str().unwrap());
}

#[unsafe(no_mangle)]
pub extern "C" fn print2(
argc: std::ffi::c_int,
argv: *const *const std::ffi::c_char
) {
if argc != 2 {
panic!("print requires 2 arguments");
}

let args = unsafe {
std::slice::from_raw_parts(argv, argc as usize)
};

let str = unsafe {
std::ffi::CStr::from_ptr(args[0])
};

let str2 = unsafe {
std::ffi::CStr::from_ptr(args[1])
};

print!("{}", str.to_str().unwrap());
print!("{}", str2.to_str().unwrap());
println!();
}


#[unsafe(no_mangle)]
pub extern "C" fn hello_world() {
println!("Hello, World!");
}
Binary file added lang/examples/ffi/libffi_test.dylib
Binary file not shown.
Binary file added lang/examples/ffi/libffi_test.so
Binary file not shown.
33 changes: 33 additions & 0 deletions lang/examples/ffi/main.modu
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "ffi" as ffi;
import "os" as os;

let path = "./libffi_test"

if os.name == "linux" {
let path = path + ".so"
}

if os.name == "macos" {
let path = path + ".dylib"
}

if os.name == "windows" {
let path = path + ".dll"
print("This may error out if you have not built ffi_test and put the .dll in same folder as main.modu!");
print("I will remove this message once i add a .dll myself there")
}

ffi.call(path, "hello_world");

print(ffi.call(path, "a", "5"));
print(ffi.call(path, "one"));

// due to some arg issues, ffi calls cant accept numbers
print(ffi.call(path, "add", str(5), str(2)));
print(ffi.call(path, "string"));

ffi.call(path, "print", "test");
ffi.call(path, "print2", "test1", " test2");


// ffi.call(path, "add", 1, 2, 3); // panic test
13 changes: 0 additions & 13 deletions lang/examples/if_exists.modu

This file was deleted.

5 changes: 5 additions & 0 deletions lang/examples/if_statements.modu
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ if c == "potato" {
print("c == 'potato'");
}

let h;
if h {
print("oh nyo");
}

// Expected Output:
//
// a == 1
Expand Down
12 changes: 0 additions & 12 deletions lang/examples/undefined_var.modu

This file was deleted.

18 changes: 2 additions & 16 deletions lang/input.modu
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import "os" as os


fn fetch(url) {
let req = "curl "+ url;
let res = os.exec(req);
print(res);
return res;
}

fn main() {
let response = fetch("https://jsonplaceholder.typicode.com/todos/1");
print(response);
}

main();
let a = 1+1
a
26 changes: 22 additions & 4 deletions lang/src/cli/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fn read_dir(dir: &std::path::Path, archive: &mut zip::ZipWriter<std::fs::File>)
}

let mut gitignore_content = String::new();

let gitignore: Result<_, _> = std::fs::File::open(".gitignore");

match gitignore {
Expand All @@ -43,6 +42,24 @@ fn read_dir(dir: &std::path::Path, archive: &mut zip::ZipWriter<std::fs::File>)
Err(_) => {}
}

let mut moduignore_content = String::new();
let moduignore: Result<_, _> = std::fs::File::open(".moduignore");

match moduignore {
Ok(mut file) => {
file.read_to_string(&mut moduignore_content).unwrap();

for line in moduignore_content.lines() {
if path.to_str().unwrap().replace("\\", "/") == format!("./{}", line) {
println!("Ignoring {}", path.to_str().unwrap());
do_break = true;
}
}
},

Err(_) => {}
}

if do_break {
continue;
}
Expand All @@ -54,9 +71,9 @@ fn read_dir(dir: &std::path::Path, archive: &mut zip::ZipWriter<std::fs::File>)

archive.start_file(name.to_str().unwrap(), zip::write::SimpleFileOptions::default()).unwrap();
let mut file = std::fs::File::open(path).unwrap();
let mut contents = String::new();
let mut contents = Vec::new();

let r = file.read_to_string(&mut contents);
let r = file.read_to_end(&mut contents);

match r {
Ok(_) => {},
Expand All @@ -65,7 +82,7 @@ fn read_dir(dir: &std::path::Path, archive: &mut zip::ZipWriter<std::fs::File>)
}
}

archive.write_all(contents.as_bytes()).unwrap();
archive.write_all(&contents).unwrap();
}
}
}
Expand Down Expand Up @@ -95,6 +112,7 @@ pub fn publish() {

std::fs::create_dir_all(".modu").unwrap();

println!("Compressing package");
let mut archive = zip::ZipWriter::new(std::fs::File::create(".modu/package.zip").unwrap());
read_dir(std::path::Path::new("."), &mut archive);
archive.finish().unwrap();
Expand Down
Loading
Loading