Skip to content

Commit a00617b

Browse files
committed
feat!: update cli, use ts for utils
1 parent b0d2a88 commit a00617b

File tree

13 files changed

+187
-254
lines changed

13 files changed

+187
-254
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ Here are some of them with syntax highlighting from the [LO VS Code extension](#
7171

7272
3️⃣ Using Node.js
7373

74-
- Install [Node.js](https://github.com/bytecodealliance/wasmtime)
75-
- Compiling files: `./utils.mjs compile <input>.lo > <output>.wasm`
76-
- Compiling & running files: `./utils.mjs run <input>.lo`
74+
- Install [Node.js 22+](https://nodejs.org/)
75+
- Compiling files: `./utils.ts compile <input>.lo > <output>.wasm`
76+
- Compiling & running files: `./utils.ts run <input>.lo`
7777

7878
---
7979

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ fn main(): u32 {
329329

330330
- Requirements:
331331
- Install [Node.js](https://nodejs.org/en/download/package-manager) for running tests
332-
- Run `./utils.mjs test`
332+
- Run `./utils.ts test`
333333

334-
> This runs tests defined in `utils.mjs`. Test programs are located in `examples/test`
334+
> This runs tests defined in `utils.ts`. Test programs are located in `examples/test`
335335
336336
> NOTE: there is currently no built-in testing solution in the compiler, existing test setup is good enough for now.

examples/lib/debug.lo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// provided by utils.mjs
1+
// provided by utils.ts
22
// useful for debugging when WASI fd_write is not available
33
import from "console" {
44
fn debug_u32::log(x: u32);

lo.wasm

1.65 KB
Binary file not shown.

src/codegen.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ struct LoConstDef {
371371
pub struct CodeGen {
372372
pub errors: LoErrorManager,
373373

374-
mode: CompilerMode,
374+
mode: LoCommand,
375375
pub fm: FileManager,
376376

377377
type_defs: Vec<LoTypeDef>,
@@ -396,7 +396,7 @@ pub struct CodeGen {
396396
}
397397

398398
impl CodeGen {
399-
pub fn new(mode: CompilerMode) -> Self {
399+
pub fn new(mode: LoCommand) -> Self {
400400
let mut codegen = Self::default();
401401
codegen.mode = mode;
402402
codegen.type_defs.push(LoTypeDef {
@@ -478,7 +478,7 @@ impl CodeGen {
478478
loc: LoLocation::internal(),
479479
});
480480

481-
if codegen.mode == CompilerMode::Inspect {
481+
if codegen.mode == LoCommand::Inspect {
482482
stdout_writeln("[");
483483
}
484484

@@ -790,7 +790,7 @@ impl CodeGen {
790790
GlobalDefValue::DataSize => LoType::U32,
791791
};
792792

793-
if self.mode == CompilerMode::Inspect {
793+
if self.mode == LoCommand::Inspect {
794794
let global_name = &global.global_name.repr;
795795

796796
self.print_inspection(&InspectInfo {
@@ -821,7 +821,7 @@ impl CodeGen {
821821
let mut const_ctx = LoExprContext::default();
822822
let code_unit = self.build_code_unit(&mut const_ctx, &const_def.const_value)?;
823823

824-
if self.mode == CompilerMode::Inspect {
824+
if self.mode == LoCommand::Inspect {
825825
let const_name = &const_def.const_name.repr;
826826
let const_type = &code_unit.lo_type;
827827

@@ -1074,7 +1074,7 @@ impl CodeGen {
10741074

10751075
wasm_module.types.append(&mut self.wasm_types.borrow_mut());
10761076

1077-
if self.mode == CompilerMode::Inspect {
1077+
if self.mode == LoCommand::Inspect {
10781078
stdout_writeln("{ \"type\": \"end\" }");
10791079
stdout_writeln("]");
10801080
}
@@ -1429,7 +1429,7 @@ impl CodeGen {
14291429

14301430
CodeExpr::Ident(ident) => {
14311431
if let Some(const_def) = self.get_const(ctx, &ident.repr) {
1432-
if self.mode == CompilerMode::Inspect {
1432+
if self.mode == LoCommand::Inspect {
14331433
self.print_inspection(&InspectInfo {
14341434
message: format!(
14351435
"const {}: {}",
@@ -2097,7 +2097,7 @@ impl CodeGen {
20972097
});
20982098
}
20992099

2100-
if self.mode == CompilerMode::Inspect {
2100+
if self.mode == LoCommand::Inspect {
21012101
let params = ListDisplay(&lo_fn_info.fn_params);
21022102
let return_type = &lo_fn_info.fn_type.output;
21032103
self.print_inspection(&InspectInfo {
@@ -2275,7 +2275,7 @@ impl CodeGen {
22752275
Some(&mut lo_type_args),
22762276
)?;
22772277

2278-
if self.mode == CompilerMode::Inspect {
2278+
if self.mode == LoCommand::Inspect {
22792279
let lo_type_args = ListDisplay(&lo_type_args);
22802280

22812281
let mut macro_args = Vec::new();
@@ -2926,7 +2926,7 @@ impl CodeGen {
29262926
loc: LoLocation,
29272927
linked_loc: Option<LoLocation>,
29282928
) -> LoVariableInfo {
2929-
let inspect_info = if self.mode == CompilerMode::Inspect {
2929+
let inspect_info = if self.mode == LoCommand::Inspect {
29302930
Some(InspectInfo {
29312931
message: format!("let {}: {}", local_name, local_type),
29322932
loc: loc.clone(),
@@ -2962,7 +2962,7 @@ impl CodeGen {
29622962
return Ok(LoVariableInfo::Global {
29632963
global_index: global.global_index,
29642964
global_type: global.global_type.clone(),
2965-
inspect_info: if self.mode == CompilerMode::Inspect {
2965+
inspect_info: if self.mode == LoCommand::Inspect {
29662966
Some(InspectInfo {
29672967
message: format!("global {}: {}", ident.repr, global.global_type),
29682968
loc: ident.loc.clone(),
@@ -2989,7 +2989,7 @@ impl CodeGen {
29892989

29902990
let field = self.get_struct_or_struct_ref_field(ctx, &lhs_type, field_access)?;
29912991

2992-
let inspect_info = if self.mode == CompilerMode::Inspect {
2992+
let inspect_info = if self.mode == LoCommand::Inspect {
29932993
Some(InspectInfo {
29942994
message: format!("{}.{}: {}", lhs_type, field.field_name, field.field_type),
29952995
loc: field_access.field_name.loc.clone(),
@@ -3148,7 +3148,7 @@ impl CodeGen {
31483148
let addr_type = self.get_expr_type(ctx, addr_expr)?;
31493149

31503150
if let LoType::Pointer { pointee } = &addr_type {
3151-
let inspect_info = if self.mode == CompilerMode::Inspect {
3151+
let inspect_info = if self.mode == LoCommand::Inspect {
31523152
Some(InspectInfo {
31533153
message: format!("<deref>: {}", pointee),
31543154
loc: op_loc.clone(),
@@ -3488,7 +3488,7 @@ impl CodeGen {
34883488

34893489
// TODO: don't use tuples
34903490
fn process_const_string(&self, value: String, loc: &LoLocation) -> Result<(u32, u32), LoError> {
3491-
if self.memory.is_none() && self.mode != CompilerMode::Inspect {
3491+
if self.memory.is_none() && self.mode != LoCommand::Inspect {
34923492
return Err(LoError {
34933493
message: format!("Cannot use strings with no memory defined"),
34943494
loc: loc.clone(),
@@ -3533,7 +3533,7 @@ impl CodeGen {
35333533

35343534
fn get_type_or_err(&self, type_name: &str, loc: &LoLocation) -> Result<LoType, LoError> {
35353535
if let Some(typedef) = self.get_typedef(type_name) {
3536-
if self.mode == CompilerMode::Inspect {
3536+
if self.mode == LoCommand::Inspect {
35373537
if typedef.loc.file_index != 0 {
35383538
if typedef.type_alias {
35393539
self.print_inspection(&InspectInfo {

src/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use alloc::{fmt, format, string::String, vec, vec::Vec};
22
use core::{cell::RefCell, default, ffi::CStr, str};
33

44
#[derive(Default, PartialEq, Clone, Copy)]
5-
pub enum CompilerMode {
5+
pub enum LoCommand {
66
#[default]
77
Compile,
88
Inspect,
9-
PrettyPrint,
9+
Format,
1010
Eval,
11-
EvalWasm,
11+
Wasi,
1212
}
1313

1414
#[derive(PartialEq)]

src/lib.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ mod wasm_target {
3434
}
3535

3636
static USAGE: &str = "\
37-
Usage: lo <file> [<mode>]
38-
Where <mode> is either:
39-
--compile (default if not provided)
40-
--inspect
41-
--pretty-print
42-
--eval (experimental)
43-
--eval-wasm (experimental)\
37+
Usage:
38+
lo compile <input.lo>
39+
lo inspect <input.lo>
40+
lo format <input.lo>
41+
lo eval <input.lo> (experimental)
42+
lo wasi <input.lo> (experimental)\
4443
";
4544

4645
mod wasi_api {
@@ -65,41 +64,27 @@ mod wasi_api {
6564

6665
fn start() -> Result<(), String> {
6766
let args = WasiArgs::load().unwrap();
68-
if args.len() < 2 {
67+
if args.len() < 3 {
6968
return Err(format!("{}", USAGE));
7069
}
7170

72-
let mut file_name = args.get(1).unwrap();
71+
let command = match args.get(1).unwrap() {
72+
"compile" => LoCommand::Compile,
73+
"inspect" => LoCommand::Inspect,
74+
"format" => LoCommand::Format,
75+
"eval" => LoCommand::Eval,
76+
"wasi" => LoCommand::Wasi,
77+
unknown_command => {
78+
return Err(format!("Unknown command: {unknown_command}\n{}", USAGE));
79+
}
80+
};
81+
82+
let mut file_name = args.get(2).unwrap();
7383
if file_name == "-i" {
7484
file_name = "<stdin>";
7585
}
7686

77-
let mut compiler_mode = CompilerMode::Compile;
78-
79-
if let Some(compiler_mode_arg) = args.get(2) {
80-
match compiler_mode_arg {
81-
"--compile" => {
82-
compiler_mode = CompilerMode::Compile;
83-
}
84-
"--inspect" => {
85-
compiler_mode = CompilerMode::Inspect;
86-
}
87-
"--pretty-print" => {
88-
compiler_mode = CompilerMode::PrettyPrint;
89-
}
90-
"--eval" => {
91-
compiler_mode = CompilerMode::Eval;
92-
}
93-
"--eval-wasm" => {
94-
compiler_mode = CompilerMode::EvalWasm;
95-
}
96-
unknown_mode => {
97-
return Err(format!("Unknown compiler mode: {unknown_mode}\n{}", USAGE));
98-
}
99-
}
100-
}
101-
102-
if compiler_mode == CompilerMode::PrettyPrint {
87+
if command == LoCommand::Format {
10388
let mut fm = FileManager::default();
10489
let included_file = fm
10590
.include_file(file_name, &LoLocation::internal())
@@ -118,7 +103,7 @@ mod wasi_api {
118103
return Ok(());
119104
}
120105

121-
if compiler_mode == CompilerMode::EvalWasm {
106+
if command == LoCommand::Wasi {
122107
let module_bytes = file_read(file_name)?;
123108

124109
let wasm_module = WasmParser::parse(String::from(file_name), module_bytes)?;
@@ -128,11 +113,11 @@ mod wasi_api {
128113
return Ok(());
129114
}
130115

131-
if compiler_mode == CompilerMode::Inspect {
116+
if command == LoCommand::Inspect {
132117
stdout_enable_buffering();
133118
}
134119

135-
let mut codegen = CodeGen::new(compiler_mode);
120+
let mut codegen = CodeGen::new(command);
136121

137122
let included_file = codegen
138123
.fm
@@ -141,7 +126,7 @@ mod wasi_api {
141126

142127
let mut asts = Vec::new();
143128
parse_file_tree(
144-
compiler_mode,
129+
command,
145130
&mut codegen.fm,
146131
&mut asts,
147132
included_file.file_index,
@@ -165,27 +150,27 @@ mod wasi_api {
165150
.generate()
166151
.map_err(|err| err.to_string(&codegen.fm))?;
167152

168-
if compiler_mode == CompilerMode::Compile {
153+
if command == LoCommand::Compile {
169154
let mut binary = Vec::new();
170155
wasm_module.dump(&mut binary);
171156
stdout_write(binary.as_slice());
172157
}
173158

174-
if compiler_mode == CompilerMode::Eval {
159+
if command == LoCommand::Eval {
175160
WasmEval::eval(wasm_module).map_err(|err| err.message)?;
176161
}
177162

178163
return Ok(());
179164
}
180165

181166
fn parse_file_tree(
182-
mode: CompilerMode,
167+
mode: LoCommand,
183168
fm: &mut FileManager,
184169
asts: &mut Vec<AST>,
185170
file_index: u32,
186171
file_contents: String,
187172
) -> Result<(), LoError> {
188-
if mode == CompilerMode::Inspect {
173+
if mode == LoCommand::Inspect {
189174
let file_path = fm.get_file_path(file_index);
190175
stdout_writeln(format!(
191176
"{{ \"type\": \"file\", \
@@ -209,7 +194,7 @@ mod wasi_api {
209194
parse_file_tree(mode, fm, asts, included_file.file_index, file_contents)?;
210195
}
211196

212-
if mode == CompilerMode::Inspect {
197+
if mode == LoCommand::Inspect {
213198
let source_index = file_index;
214199
let source_range = RangeDisplay(&include.loc);
215200
let target_index = included_file.file_index;

0 commit comments

Comments
 (0)