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",
"failure",
"itertools",
"lazy_static 1.4.0",
"maplit",
"radix_trie",
"schala-lang-codegen",
"schala-repl",

View File

@ -8,8 +8,6 @@ resolver = "2"
[dependencies]
itertools = "0.10"
take_mut = "0.2.2"
maplit = "1.0.1"
lazy_static = "1.3.0"
failure = "0.1.5"
ena = "0.11.0"
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
//! `ProgrammingLanguageInterface` and the chain of compiler passes for it.
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate maplit;
extern crate schala_repl;
#[macro_use]
extern crate schala_lang_codegen;

View File

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

View File

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

View File

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

View File

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

View File

@ -2,11 +2,11 @@ use std::collections::HashSet;
use std::time;
pub trait ProgrammingLanguageInterface {
type Options;
type Config: Default;
fn language_name() -> 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 {
LangMetaResponse::Custom {
@ -23,9 +23,9 @@ struct Options<T> {
}
*/
pub struct ComputationRequest<'a> {
pub struct ComputationRequest<'a, T> {
pub source: &'a str,
//pub options: Options<()>,
pub config: T,
pub debug_requests: HashSet<DebugAsk>,
}

View File

@ -187,6 +187,7 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
let request = ComputationRequest {
source: input,
config: Default::default(),
debug_requests,
};
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 {
source: &buffer,
config: Default::default(),
debug_requests: HashSet::new(),
};