Compare commits

..

3 Commits

Author SHA1 Message Date
Greg Shuflin
421a33c42c Use TryFrom<&str> for Tokens 2021-10-14 02:47:19 -07:00
Greg Shuflin
61e2acc338 Parameterize compiler Config type 2021-10-14 02:24:42 -07:00
Greg Shuflin
2d72f560ed Simplify directive types 2021-10-14 02:20:11 -07:00
10 changed files with 35 additions and 53 deletions

2
Cargo.lock generated
View File

@ -836,8 +836,6 @@ dependencies = [
"ena", "ena",
"failure", "failure",
"itertools", "itertools",
"lazy_static 1.4.0",
"maplit",
"radix_trie", "radix_trie",
"schala-lang-codegen", "schala-lang-codegen",
"schala-repl", "schala-repl",

View File

@ -8,8 +8,6 @@ resolver = "2"
[dependencies] [dependencies]
itertools = "0.10" itertools = "0.10"
take_mut = "0.2.2" take_mut = "0.2.2"
maplit = "1.0.1"
lazy_static = "1.3.0"
failure = "0.1.5" failure = "0.1.5"
ena = "0.11.0" ena = "0.11.0"
stopwatch = "0.0.7" stopwatch = "0.0.7"

View File

@ -6,10 +6,6 @@
//! It defines the `Schala` type, which contains the state for a Schala REPL, and implements //! It defines the `Schala` type, which contains the state for a Schala REPL, and implements
//! `ProgrammingLanguageInterface` and the chain of compiler passes for it. //! `ProgrammingLanguageInterface` and the chain of compiler passes for it.
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate maplit;
extern crate schala_repl; extern crate schala_repl;
#[macro_use] #[macro_use]
extern crate schala_lang_codegen; extern crate schala_lang_codegen;

View File

@ -2,7 +2,6 @@ use stopwatch::Stopwatch;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::collections::HashSet;
use schala_repl::{ProgrammingLanguageInterface, use schala_repl::{ProgrammingLanguageInterface,
ComputationRequest, ComputationResponse, ComputationRequest, ComputationResponse,
@ -58,15 +57,13 @@ impl Schala {
/// Schala code in the file `prelude.schala` /// Schala code in the file `prelude.schala`
pub fn new() -> Schala { pub fn new() -> Schala {
let prelude = include_str!("prelude.schala"); let prelude = include_str!("prelude.schala");
let mut s = Schala::new_blank_env(); let mut env = Schala::new_blank_env();
//TODO this shouldn't depend on schala-repl types let response = env.run_pipeline(prelude);
let request = ComputationRequest { source: prelude, debug_requests: HashSet::default() }; if let Err(msg) = response {
let response = s.run_computation(request);
if let Err(msg) = response.main_output {
panic!("Error in prelude, panicking: {}", msg); panic!("Error in prelude, panicking: {}", msg);
} }
s env
} }
/// This is where the actual action of interpreting/compilation happens. /// This is where the actual action of interpreting/compilation happens.
@ -174,7 +171,7 @@ fn stage_names() -> Vec<&'static str> {
impl ProgrammingLanguageInterface for Schala { impl ProgrammingLanguageInterface for Schala {
type Options = (); type Config = ();
fn language_name() -> String { fn language_name() -> String {
"Schala".to_owned() "Schala".to_owned()
} }
@ -183,8 +180,8 @@ impl ProgrammingLanguageInterface for Schala {
"schala".to_owned() "schala".to_owned()
} }
fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse { fn run_computation(&mut self, request: ComputationRequest<Self::Config>) -> ComputationResponse {
let ComputationRequest { source, debug_requests: _ } = request; let ComputationRequest { source, debug_requests: _, config: _ } = request;
self.source_reference.load_new_source(source); self.source_reference.load_new_source(source);
let sw = Stopwatch::start_new(); let sw = Stopwatch::start_new();

View File

@ -1,8 +1,5 @@
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashMap; use std::{iter::{Iterator, Peekable}, convert::TryFrom, rc::Rc, fmt};
use std::rc::Rc;
use std::iter::{Iterator, Peekable};
use std::fmt;
use crate::source_map::Location; use crate::source_map::Location;
@ -63,9 +60,11 @@ pub enum Kw {
Module, Import Module, Import
} }
lazy_static! { impl TryFrom<&str> for Kw {
static ref KEYWORDS: HashMap<&'static str, Kw> = type Error = ();
hashmap! {
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"if" => Kw::If, "if" => Kw::If,
"then" => Kw::Then, "then" => Kw::Then,
"else" => Kw::Else, "else" => Kw::Else,
@ -88,7 +87,9 @@ lazy_static! {
"false" => Kw::False, "false" => Kw::False,
"module" => Kw::Module, "module" => Kw::Module,
"import" => Kw::Import, "import" => Kw::Import,
}; _ => return Err(()),
})
}
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -239,9 +240,9 @@ fn handle_alphabetic(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>
} }
} }
match KEYWORDS.get(buf.as_str()) { match Kw::try_from(buf.as_str()) {
Some(kw) => TokenKind::Keyword(*kw), Ok(kw) => TokenKind::Keyword(kw),
None => TokenKind::Identifier(Rc::new(buf)), Err(()) => TokenKind::Identifier(Rc::new(buf)),
} }
} }

View File

@ -11,10 +11,8 @@ pub enum DirectiveAction {
Help, Help,
QuitProgram, QuitProgram,
ListPasses, ListPasses,
TotalTimeOff, TotalTime(bool),
TotalTimeOn, StageTime(bool),
StageTimeOff,
StageTimeOn,
Doc, Doc,
} }
@ -50,20 +48,12 @@ impl DirectiveAction {
} }
Some(buf) Some(buf)
} }
TotalTimeOff => { TotalTime(value) => {
repl.options.show_total_time = false; repl.options.show_total_time = *value;
None None
} }
TotalTimeOn => { StageTime(value) => {
repl.options.show_total_time = true; repl.options.show_stage_times = *value;
None
}
StageTimeOff => {
repl.options.show_stage_times = false;
None
}
StageTimeOn => {
repl.options.show_stage_times = true;
None None
} }
Doc => doc(repl, arguments), Doc => doc(repl, arguments),

View File

@ -54,16 +54,16 @@ fn get_list(passes_directives: &[CommandTree], include_help: bool) -> Vec<Comman
"total-time", "total-time",
None, None,
vec![ vec![
CommandTree::terminal("on", None, vec![], TotalTimeOn), CommandTree::terminal("on", None, vec![], TotalTime(true)),
CommandTree::terminal("off", None, vec![], TotalTimeOff), CommandTree::terminal("off", None, vec![], TotalTime(false)),
], ],
), ),
CommandTree::nonterm( CommandTree::nonterm(
"stage-times", "stage-times",
Some("Computation time per-stage"), Some("Computation time per-stage"),
vec![ vec![
CommandTree::terminal("on", None, vec![], StageTimeOn), CommandTree::terminal("on", None, vec![], StageTime(true)),
CommandTree::terminal("off", None, vec![], StageTimeOff), CommandTree::terminal("off", None, vec![], StageTime(false)),
], ],
), ),
], ],

View File

@ -2,11 +2,11 @@ use std::collections::HashSet;
use std::time; use std::time;
pub trait ProgrammingLanguageInterface { pub trait ProgrammingLanguageInterface {
type Options; type Config: Default;
fn language_name() -> String; fn language_name() -> String;
fn source_file_suffix() -> String; fn source_file_suffix() -> String;
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse; fn run_computation(&mut self, _request: ComputationRequest<Self::Config>) -> ComputationResponse;
fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse { fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse {
LangMetaResponse::Custom { LangMetaResponse::Custom {
@ -23,9 +23,9 @@ struct Options<T> {
} }
*/ */
pub struct ComputationRequest<'a> { pub struct ComputationRequest<'a, T> {
pub source: &'a str, pub source: &'a str,
//pub options: Options<()>, pub config: T,
pub debug_requests: HashSet<DebugAsk>, pub debug_requests: HashSet<DebugAsk>,
} }

View File

@ -187,6 +187,7 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
let request = ComputationRequest { let request = ComputationRequest {
source: input, source: input,
config: Default::default(),
debug_requests, debug_requests,
}; };
let response = self.language_state.run_computation(request); let response = self.language_state.run_computation(request);

View File

@ -52,6 +52,7 @@ pub fn run_noninteractive<L: ProgrammingLanguageInterface>(filenames: Vec<PathBu
let request = ComputationRequest { let request = ComputationRequest {
source: &buffer, source: &buffer,
config: Default::default(),
debug_requests: HashSet::new(), debug_requests: HashSet::new(),
}; };