Skip to content

Commit

Permalink
Implement std.import
Browse files Browse the repository at this point in the history
Also add script command line arg
  • Loading branch information
gahag committed Sep 1, 2021
1 parent 4de2d42 commit 3067e82
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 113 deletions.
2 changes: 1 addition & 1 deletion examples/hush/heap.hsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# * array: the data array
# * cmp: function or nil -- the comparison function, defaults to ascending
# Returns the heap instance.
let Heap = function(array, cmp)
function(array, cmp)
if cmp == nil then
cmp = function (a, b)
a < b
Expand Down
11 changes: 10 additions & 1 deletion examples/hush/iterator.hsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# iterator: traversal utilities.

let Heap # TODO: import Heap module.
let Heap = std.import("./heap.hsh")

# Construct an iterator.
# An Iterator is an object capable of iterating a collection.
Expand Down Expand Up @@ -293,3 +293,12 @@ function Chain(iterators)
end
)
end


@[
Iterator: Iterator,
Array: Array,
Dict: Dict,
Empty: Empty,
Chain: Chain,
]
33 changes: 24 additions & 9 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::ffi::OsString;
use std::{
ffi::OsString,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
};

use clap::{clap_app, crate_authors, crate_version, crate_description};

Expand All @@ -13,6 +17,7 @@ pub enum Command {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Args {
pub script_path: Option<PathBuf>,
/// Check program with static analysis, but don't run.
pub check: bool,
/// Print the AST.
Expand All @@ -32,21 +37,31 @@ where
(version: crate_version!())
(author: crate_authors!())
(about: crate_description!())
(@arg script_path: "the script to execute")
(@arg check: --check "Perform only static analysis instead of executing.")
(@arg ast: --ast "Print the AST")
(@arg program: --program "Print the PROGAM")
);

match app.get_matches_from_safe(args) {
Ok(matches) => Ok(
Command::Run(
Args {
check: matches.is_present("check"),
print_ast: matches.is_present("ast"),
print_program: matches.is_present("program"),
}
Ok(matches) => {
let script_path = match matches.value_of_os("script_path") {
Some(path) if path.as_bytes() == b"-" => None,
Some(path) => Some(Path::new(path).into()),
None => None,
};

Ok(
Command::Run(
Args {
script_path,
check: matches.is_present("check"),
print_ast: matches.is_present("ast"),
print_program: matches.is_present("program"),
}
)
)
),
},

Err(error) => match error.kind {
clap::ErrorKind::HelpDisplayed => Ok(
Expand Down
62 changes: 42 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod term;
#[cfg(test)]
mod tests;

use std::os::unix::ffi::OsStrExt;

use term::color;

use args::{Args, Command};
Expand Down Expand Up @@ -61,25 +63,47 @@ fn main() -> ! {

fn run(args: Args) -> ExitStatus {
let mut interner = symbol::Interner::new();
let path = interner.get_or_intern("<stdin>");

let source = match syntax::Source::from_reader(path, std::io::stdin().lock()) {
let (source, path) = match args.script_path {
Some(path) => {
let path = interner.get_or_intern(path.as_os_str().as_bytes());
let source = syntax::Source::from_path(path, &mut interner);
(source, path)
},

None => {
let path = interner.get_or_intern("<stdin>");
let source = syntax::Source::from_reader(path, std::io::stdin().lock());
(source, path)
},
};

let source = match source {
Ok(source) => source,
Err(error) => {
eprintln!("{}", fmt::Show(Panic::io(error, SourcePos::file(path)), &interner));
eprintln!(
"{}",
fmt::Show(
Panic::io(error, SourcePos::file(path)),
&interner
)
);
return ExitStatus::Panic;
}
};

// ----------------------------------------------------------------------------------------
let syntactic_analysis = syntax::Analysis::analyze(source, &mut interner);

for error in syntactic_analysis.errors.iter().take(20) {
eprintln!(
"{}: {}",
color::Fg(color::Red, "Error"),
fmt::Show(error, &interner)
);
let has_syntax_errors = !syntactic_analysis.is_ok();

if has_syntax_errors {
eprint!("{}", fmt::Show(
syntactic_analysis.errors,
syntax::AnalysisDisplayContext {
max_errors: Some(20),
interner: &interner,
}
));
}

if args.print_ast {
Expand All @@ -97,16 +121,14 @@ fn run(args: Args) -> ExitStatus {
// ----------------------------------------------------------------------------------------
let program = match semantic::Analyzer::analyze(syntactic_analysis.ast, &mut interner) {
Ok(program) => program,

Err(errors) => {
for error in errors.into_iter().take(20) {
eprintln!(
"{}: {}",
color::Fg(color::Red, "Error"),
fmt::Show(error, &interner)
);
}

eprint!("{}", fmt::Show(
errors,
semantic::ErrorsDisplayContext {
max_errors: Some(20),
interner: &interner,
}
));
return ExitStatus::StaticError;
}
};
Expand All @@ -124,7 +146,7 @@ fn run(args: Args) -> ExitStatus {
}

// ----------------------------------------------------------------------------------------
if !syntactic_analysis.errors.is_empty() {
if has_syntax_errors {
return ExitStatus::StaticError;
}

Expand Down
Loading

0 comments on commit 3067e82

Please sign in to comment.