cl 0.0.2: MAJOR ERGONOMIC BOOST
Broke frontend into its own library, "cl-frontend" - Frontend is pretty :D - Included sample fibonacci implementation Deprecated conlang::ast::Visitor in favor of bespoke traits - Rust traits are super cool. - The Interpreter is currently undergoing a major rewrite Added preliminary type-path support to the parser - Currently incomplete: type paths must end in Never..? Pretty printer is now even prettier - conlang::ast now exports all relevant AST nodes, since there are no namespace collisions any more
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
//! This example grabs input from stdin, lexes it, parses it, and prints the AST
|
||||
#![allow(unused_imports)]
|
||||
use conlang::{lexer::Lexer, parser::Parser, pretty_printer::PrettyPrintable, token::Token};
|
||||
use std::{
|
||||
error::Error,
|
||||
io::{stdin, stdout, IsTerminal, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let conf = Config::new();
|
||||
if conf.paths.is_empty() {
|
||||
take_stdin()?;
|
||||
} else {
|
||||
for path in conf.paths.iter().map(PathBuf::as_path) {
|
||||
parse(&std::fs::read_to_string(path)?, Some(path));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Config {
|
||||
paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new() -> Self {
|
||||
Config { paths: std::env::args().skip(1).map(PathBuf::from).collect() }
|
||||
}
|
||||
}
|
||||
|
||||
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
||||
const PROMPT: &str = "> ";
|
||||
if stdin().is_terminal() {
|
||||
print!("{PROMPT}");
|
||||
stdout().flush()?;
|
||||
for line in stdin().lines() {
|
||||
let line = line?;
|
||||
if !line.is_empty() {
|
||||
parse(&line, None);
|
||||
println!();
|
||||
}
|
||||
print!("{PROMPT}");
|
||||
stdout().flush()?;
|
||||
}
|
||||
} else {
|
||||
parse(&std::io::read_to_string(stdin())?, None)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse(file: &str, path: Option<&Path>) {
|
||||
use conlang::parser::error::Error;
|
||||
match Parser::from(Lexer::new(file)).parse() {
|
||||
Ok(ast) => ast.print(),
|
||||
Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e),
|
||||
Err(e) => print!("{e}"),
|
||||
}
|
||||
println!();
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
//! This example grabs input from stdin, lexes it, and prints which lexer rules matched
|
||||
#![allow(unused_imports)]
|
||||
use conlang::lexer::Lexer;
|
||||
use std::{
|
||||
error::Error,
|
||||
io::{stdin, IsTerminal, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let conf = Config::new();
|
||||
if conf.paths.is_empty() {
|
||||
take_stdin()?;
|
||||
} else {
|
||||
for path in conf.paths.iter().map(PathBuf::as_path) {
|
||||
lex_tokens(&std::fs::read_to_string(path)?, Some(path))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Config {
|
||||
paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new() -> Self {
|
||||
Config { paths: std::env::args().skip(1).map(PathBuf::from).collect() }
|
||||
}
|
||||
}
|
||||
|
||||
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
||||
if stdin().is_terminal() {
|
||||
for line in stdin().lines() {
|
||||
lex_tokens(&line?, None)?
|
||||
}
|
||||
} else {
|
||||
lex_tokens(&std::io::read_to_string(stdin())?, None)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn lex_tokens(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
|
||||
for token in Lexer::new(file) {
|
||||
let token = match token {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("{e:?}");
|
||||
continue;
|
||||
},
|
||||
};
|
||||
if let Some(path) = path {
|
||||
print!("{path:?}:")
|
||||
}
|
||||
print_token(token);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_token(t: conlang::token::Token) {
|
||||
println!(
|
||||
"{:02}:{:02}: {:#19} │{}│",
|
||||
t.line(),
|
||||
t.col(),
|
||||
t.ty(),
|
||||
t.data(),
|
||||
)
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
//! This example grabs input from stdin or a file, lexes it, parses it, and interprets it
|
||||
use conlang::{
|
||||
ast::{expression::Expr, Start},
|
||||
interpreter::Interpreter,
|
||||
lexer::Lexer,
|
||||
parser::Parser,
|
||||
};
|
||||
use std::{
|
||||
error::Error,
|
||||
io::{stdin, stdout, IsTerminal, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let conf = Config::new();
|
||||
if conf.paths.is_empty() {
|
||||
take_stdin()?;
|
||||
} else {
|
||||
for path in conf.paths.iter().map(PathBuf::as_path) {
|
||||
run_file(&std::fs::read_to_string(path)?, Some(path))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Config {
|
||||
paths: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new() -> Self {
|
||||
Config { paths: std::env::args().skip(1).map(PathBuf::from).collect() }
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! prompt {
|
||||
($($t:tt)*) => {{
|
||||
let mut out = stdout().lock();
|
||||
out.write_all(b"\x1b[36m")?;
|
||||
write!(out, $($t)*)?;
|
||||
out.write_all(b"\x1b[0m")?;
|
||||
out.flush()
|
||||
}};
|
||||
}
|
||||
|
||||
fn take_stdin() -> Result<(), Box<dyn Error>> {
|
||||
const CONLANG: &str = "cl>";
|
||||
const MOREPLS: &str = " ?>";
|
||||
if stdin().is_terminal() {
|
||||
let mut interpreter = Interpreter::new();
|
||||
let mut buf = String::new();
|
||||
prompt!("{CONLANG} ")?;
|
||||
while let Ok(len) = stdin().read_line(&mut buf) {
|
||||
let code = Program::parse(&buf);
|
||||
match (len, code) {
|
||||
// Exit the loop
|
||||
(0, _) => {
|
||||
println!();
|
||||
break;
|
||||
}
|
||||
// If the code is OK, run it and print any errors
|
||||
(_, Ok(code)) => {
|
||||
let _ = code.run(&mut interpreter).map_err(|e| eprintln!("{e}"));
|
||||
buf.clear();
|
||||
}
|
||||
// If the user types two newlines, print syntax errors
|
||||
(1, Err(e)) => {
|
||||
eprintln!("{e}");
|
||||
buf.clear();
|
||||
}
|
||||
// Otherwise, ask for more input
|
||||
_ => {
|
||||
prompt!("{MOREPLS} ")?;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
prompt!("{CONLANG} ")?;
|
||||
}
|
||||
} else {
|
||||
run_file(&std::io::read_to_string(stdin())?, None)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_file(file: &str, path: Option<&Path>) -> Result<(), Box<dyn Error>> {
|
||||
let mut interpreter = Interpreter::new();
|
||||
match Parser::from(Lexer::new(file)).parse() {
|
||||
Ok(ast) => {
|
||||
interpreter.interpret(&ast)?;
|
||||
println!("{}", interpreter.call("main", &[])?)
|
||||
},
|
||||
Err(e) if e.start().is_some() => print!("{:?}:{}", path.unwrap_or(Path::new("-")), e),
|
||||
Err(e) => print!("{e}"),
|
||||
}
|
||||
println!();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
enum Program {
|
||||
Program(Start),
|
||||
Expr(Expr),
|
||||
}
|
||||
|
||||
impl Program {
|
||||
fn parse(input: &str) -> conlang::parser::error::PResult<Self> {
|
||||
let ast = Parser::from(Lexer::new(input)).parse();
|
||||
if let Ok(ast) = ast {
|
||||
return Ok(Self::Program(ast));
|
||||
}
|
||||
Ok(Self::Expr(Parser::from(Lexer::new(input)).parse_expr()?))
|
||||
}
|
||||
fn run(&self, interpreter: &mut Interpreter) -> conlang::interpreter::error::IResult<()> {
|
||||
match self {
|
||||
Program::Program(start) => interpreter.interpret(start),
|
||||
Program::Expr(expr) => {
|
||||
for value in interpreter.eval(expr)? {
|
||||
println!("{value}")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user