Skip to content

Commit

Permalink
feat(WIP): file imports
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Sep 28, 2023
1 parent 0811e71 commit a942ecd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 37 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 20 additions & 15 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tokio::sync::RwLock;
mod lsp;
mod util;


use lsp::WhistleBackend;

use tower_lsp::{LspService, Server};
Expand Down Expand Up @@ -39,9 +38,7 @@ enum Commands {

/// compiles and runs the code
#[command(arg_required_else_help = true)]
Run {
path: String,
},
Run { path: String },

/// compiles the file
Compile {
Expand All @@ -64,8 +61,9 @@ async fn main() {
match args.command {
Commands::Lex { path } => {
let now = Instant::now();
let path = &std::path::Path::new::<String>(&path);
let text = fs::read_to_string(path).expect("Something went wrong, we can't read this file.");
let (_, tokens) = util::preprocess(&text, false);
let (_, tokens) = util::preprocess(path, &text, false);
println!("{:#?}", tokens);
println!(
"Operation complete! Took us about {} seconds.",
Expand All @@ -75,8 +73,9 @@ async fn main() {

Commands::Parse { path } => {
let now = Instant::now();
let path = &std::path::Path::new::<String>(&path);
let text = fs::read_to_string(path).expect("Something went wrong, we can't read this file.");
let ast = util::parse(&text, false);
let ast = util::parse(path, &text, false);
println!("{:#?}", ast);
println!(
"Operation complete! Took us about {} seconds.",
Expand All @@ -85,30 +84,36 @@ async fn main() {
}

Commands::Run { path } => {
let path = &std::path::Path::new::<String>(&path);
let text = fs::read_to_string(path).expect("Something went wrong, we can't read this file.");
let bytes = util::compile(&text);
let bytes = util::compile(path, &text);
let engine = wasmtime::Engine::default();
let mut linker = wasmtime::Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |s| s).unwrap();
let wasi = wasmtime_wasi::WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args().unwrap()
.build();
.inherit_stdio()
.inherit_args()
.unwrap()
.build();
let mut store = wasmtime::Store::new(&engine, wasi);

let module = wasmtime::Module::new(&engine, &bytes).unwrap();
linker.module(&mut store, "", &module).unwrap();
linker
.get_default(&mut store, "").unwrap()
.typed::<(), ()>(&store).unwrap()
.call(&mut store, ()).unwrap();
.get_default(&mut store, "")
.unwrap()
.typed::<(), ()>(&store)
.unwrap()
.call(&mut store, ())
.unwrap();
}

Commands::Compile { path, output } => {
let now = Instant::now();
let output = output.unwrap_or(path.replace(".whi", ".wasm"));
let path = &std::path::Path::new::<String>(&path);
let text = fs::read_to_string(path).expect("Something went wrong, we can't read this file.");
let bytes = util::compile(&text);
let bytes = util::compile(path, &text);
if output.ends_with(".wat") {
let wasm_text = wasmprinter::print_bytes(&bytes).unwrap();
fs::write(output, wasm_text.as_bytes())
Expand Down
20 changes: 11 additions & 9 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::path;

use whistle_ast::Grammar;
use whistle_common::{DiagnosticHandler, TokenItem};
use whistle_compiler::*;
use whistle_parser::*;
use whistle_preprocessor::Preprocessor;

pub fn preprocess(text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
pub fn preprocess(path: &path::Path, text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
let handler = DiagnosticHandler::new();
let mut processor = Preprocessor::new(handler);
processor.process(text);
processor.process(text, path);
handle_errors(&mut processor.handler);
let tokens = processor.finalize();

Expand All @@ -18,8 +20,8 @@ pub fn preprocess(text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
(processor, tokens)
}

pub fn parse(text: &str, print: bool) -> (Parser, Grammar) {
let (processor, tokens) = preprocess(text, false);
pub fn parse(path: &path::Path, text: &str, print: bool) -> (Parser, Grammar) {
let (processor, tokens) = preprocess(path, text, false);
let mut parser = Parser::new(processor, tokens);
let grammar = parse_all(&mut parser);
handle_errors(&mut parser.handler);
Expand All @@ -31,17 +33,17 @@ pub fn parse(text: &str, print: bool) -> (Parser, Grammar) {
(parser, grammar)
}

pub fn check(text: &str) -> (Checker, Grammar) {
let (parser, mut grammar) = parse(text, false);
pub fn check(path: &path::Path, text: &str) -> (Checker, Grammar) {
let (parser, mut grammar) = parse(path, text, false);
let mut checker = Checker::new(parser);
check_all(&mut checker, &mut grammar);
handle_errors(&mut checker.handler);

(checker, grammar)
}

pub fn compile(text: &str) -> Vec<u8> {
let (checker, grammar) = check(text);
pub fn compile(path: &path::Path, text: &str) -> Vec<u8> {
let (checker, grammar) = check(path, text);
let mut compiler = Compiler::new(checker);
let res = compile_all(&mut compiler, grammar);
handle_errors(&mut compiler.handler);
Expand All @@ -55,4 +57,4 @@ pub fn handle_errors(handler: &mut DiagnosticHandler) {
println!("{:#?}", handler.errors);
std::process::exit(1);
};
}
}
2 changes: 1 addition & 1 deletion examples/import.whi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { add } from "./add.whi"
import "../examples/add.whi"

export fn helloWorld(): i32 {
return add(1, 2)
Expand Down
4 changes: 2 additions & 2 deletions fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub struct ParsedSource {
}

pub fn format_text(
_file_path: &Path,
file_path: &Path,
file_text: &str,
config: &Configuration,
) -> Result<Option<String>> {
if utils::file_text_has_ignore_comment(file_text, "// whistle-ignore-file") {
Ok(None)
} else {
let parsed_source = utils::parse(file_text, false);
let parsed_source = utils::parse(file_path, file_text, false);
let parsed_source = ParsedSource {
parser: parsed_source.0,
grammar: parsed_source.1,
Expand Down
12 changes: 6 additions & 6 deletions fmt/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

use std::path;

use whistle_ast::Grammar;
use whistle_common::{DiagnosticHandler, TokenItem};
Expand All @@ -15,10 +15,10 @@ pub fn file_text_has_ignore_comment(file_text: &str, ignore_file_comment_text: &
has_ignore_comment
}

pub fn preprocess(text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
pub fn preprocess(path: &path::Path, text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
let handler = DiagnosticHandler::new();
let mut processor = Preprocessor::new(handler);
processor.process(text);
processor.process(text, path);
handle_errors(&mut processor.handler);
let tokens = processor.finalize();

Expand All @@ -29,8 +29,8 @@ pub fn preprocess(text: &str, print: bool) -> (Preprocessor, Vec<TokenItem>) {
(processor, tokens)
}

pub fn parse(text: &str, print: bool) -> (Parser, Grammar) {
let (processor, tokens) = preprocess(text, false);
pub fn parse(path: &path::Path, text: &str, print: bool) -> (Parser, Grammar) {
let (processor, tokens) = preprocess(path, text, false);
let mut parser = Parser::new(processor, tokens);
let grammar = parse_all(&mut parser);
handle_errors(&mut parser.handler);
Expand All @@ -47,4 +47,4 @@ pub fn handle_errors(handler: &mut DiagnosticHandler) {
if handler.errors.len() > 0 {
println!("{:#?}", handler.errors);
};
}
}
3 changes: 2 additions & 1 deletion preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ edition = "2021"
whistle_lexer = { path = "../lexer" }
whistle_common ={ path = "../common"}
reqwest = { version = "0.11.20", features = ["blocking"] }
url = "2.4.1"
url = "2.4.1"
resolve-path = "0.1.0"
18 changes: 15 additions & 3 deletions preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use resolve_path::PathResolveExt;
use std::path;
use whistle_common::DiagnosticHandler;
use whistle_common::Keyword;
use whistle_common::LexerHandler;
Expand All @@ -22,7 +24,7 @@ impl Preprocessor {
}
}

pub fn process(&mut self, src: &str) {
pub fn process(&mut self, src: &str, path: &path::Path) {
let mut lexer = Lexer::new(src);
let mut tokens: Vec<TokenItem> = Vec::new();

Expand Down Expand Up @@ -68,6 +70,16 @@ impl Preprocessor {
file_name =
"https://raw.githubusercontent.com/whistle-lang/std/main/".to_owned() + &file_name;
}

let new_file_name = if url::Url::parse(&file_name).is_err() {
match file_name.resolve_in(&path.canonicalize().unwrap()) {
std::borrow::Cow::Owned(v) => v,
std::borrow::Cow::Borrowed(v) => v.to_owned(),
}
} else {
path.to_path_buf()
};

let file_data = if url::Url::parse(&file_name).is_ok() {
match reqwest::blocking::get(file_name) {
Ok(v) => match v.text() {
Expand All @@ -85,7 +97,7 @@ impl Preprocessor {
}
}
} else {
match std::fs::read_to_string(file_name) {
match std::fs::read_to_string(&new_file_name) {
Ok(v) => v,
Err(_) => {
return self
Expand All @@ -94,7 +106,7 @@ impl Preprocessor {
}
}
};
self.process(&file_data);
self.process(&file_data, &new_file_name);
}

self.token_list.push(tokens);
Expand Down

0 comments on commit a942ecd

Please sign in to comment.