Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
segeljakt committed May 17, 2024
1 parent fa1f60b commit aa8e537
Show file tree
Hide file tree
Showing 28 changed files with 1,029 additions and 720 deletions.
12 changes: 12 additions & 0 deletions crates/aqua-ls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "aqua-ls"
version.workspace = true
edition.workspace = true

[dependencies]
compiler = { path = "../compiler" }

lsp-server = "0.7.6"
lsp-types = "0.95.0"
serde_json = "1.0.113"
serde = { version = "1.0.113", features = ["derive"] }
91 changes: 91 additions & 0 deletions crates/aqua-ls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::error::Error;

use lsp_types::request::GotoDefinition;
use lsp_types::DiagnosticOptions;
use lsp_types::DiagnosticServerCapabilities;
use lsp_types::GotoDefinitionResponse;
use lsp_types::InitializeParams;
use lsp_types::ServerCapabilities;

use lsp_server::Connection;
use lsp_server::ExtractError;
use lsp_server::Message;
use lsp_server::Request;
use lsp_server::RequestId;
use lsp_server::Response;

fn main() -> Result<(), Box<dyn Error + Sync + Send>> {
let (connection, io_threads) = Connection::stdio();
let server_capabilities = serde_json::to_value(ServerCapabilities {
diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
identifier: None,
inter_file_dependencies: true,
workspace_diagnostics: false,
work_done_progress_options: lsp_types::WorkDoneProgressOptions {
work_done_progress: None,
},
})),
..ServerCapabilities::default()
})
.unwrap();
let initialization_params = match connection.initialize(server_capabilities) {
Ok(it) => it,
Err(e) => {
if e.channel_is_disconnected() {
io_threads.join()?;
}
return Err(e.into());
}
};
main_loop(connection, initialization_params)?;
io_threads.join()?;
Ok(())
}

fn main_loop(
connection: Connection,
params: serde_json::Value,
) -> Result<(), Box<dyn Error + Sync + Send>> {
let _params: InitializeParams = serde_json::from_value(params).unwrap();
for msg in &connection.receiver {
match msg {
Message::Request(req) => {
if connection.handle_shutdown(&req)? {
return Ok(());
}
match cast::<GotoDefinition>(req) {
Ok((id, params)) => {
eprintln!("got gotoDefinition request #{id}: {params:?}");
let result = Some(GotoDefinitionResponse::Array(Vec::new()));
let result = serde_json::to_value(&result).unwrap();
let resp = Response {
id,
result: Some(result),
error: None,
};
connection.sender.send(Message::Response(resp))?;
continue;
}
Err(err @ ExtractError::JsonError { .. }) => panic!("{err:?}"),
Err(ExtractError::MethodMismatch(req)) => req,
};
// ...
}
Message::Response(resp) => {
eprintln!("got response: {resp:?}");
}
Message::Notification(not) => {
eprintln!("got notification: {not:?}");
}
}
}
Ok(())
}

fn cast<R>(req: Request) -> Result<(RequestId, R::Params), ExtractError<Request>>
where
R: lsp_types::request::Request,
R::Params: serde::de::DeserializeOwned,
{
req.extract(R::METHOD)
}
8 changes: 4 additions & 4 deletions crates/compiler/src/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::ast::StmtType;
use crate::ast::StmtTypeBody;
use crate::ast::StmtVar;
use crate::ast::TraitBound;
use crate::ast::TraitDef;
use crate::ast::StmtTraitDef;
use crate::ast::Type;
use crate::ast::UnresolvedPatField;
use crate::infer::Context;
Expand Down Expand Up @@ -252,15 +252,15 @@ impl StmtTrait {
}
}

impl TraitDef {
pub fn annotate(&self, ctx: &mut Context) -> TraitDef {
impl StmtTraitDef {
pub fn annotate(&self, ctx: &mut Context) -> StmtTraitDef {
let span = self.span;
let name = self.name;
let generics = self.generics.clone();
let where_clause = self.where_clause.iter().map(|p| p.annotate(ctx)).collect();
let params = self.params.map_values(|t| t.annotate(ctx));
let ty = self.ty.annotate(ctx);
TraitDef::new(span, name, generics, params, ty, where_clause)
StmtTraitDef::new(span, name, generics, params, ty, where_clause)
}
}

Expand Down
46 changes: 42 additions & 4 deletions crates/compiler/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::ast::StmtType;
use crate::ast::StmtTypeBody;
use crate::ast::StmtVar;
use crate::ast::TraitBound;
use crate::ast::TraitDef;
use crate::ast::StmtTraitDef;
use crate::ast::Type;
use crate::map::Map;

Expand Down Expand Up @@ -76,6 +76,44 @@ impl Type {
}
}

pub fn expand_assoc(&self) -> Type {
match self {
Type::Cons(x, ts) => {
let ts = ts.iter().map(|t| t.expand_assoc()).collect::<Vec<_>>();
Type::Cons(*x, ts)
}
Type::Assoc(b, x1, _) => b.xts.get(x1).unwrap().expand_assoc(),
Type::Err => Type::Err,
Type::Generic(x) => Type::Generic(*x),
Type::Fun(ts, t) => {
let ts = ts.iter().map(|t| t.expand_assoc()).collect();
let t = t.expand_assoc();
Type::Fun(ts, Rc::new(t))
}
Type::Tuple(ts) => {
let ts = ts.iter().map(|t| t.expand_assoc()).collect();
Type::Tuple(ts)
}
Type::Record(xts) => {
let xts = xts.iter().map(|(x, t)| (*x, t.expand_assoc())).collect();
Type::Record(xts)
}
Type::Alias(x, ts) => {
let ts = ts.iter().map(|t| t.expand_assoc()).collect();
Type::Alias(*x, ts)
}
Type::Array(t, n) => {
let t = Rc::new(t.expand_assoc());
let n = *n;
Type::Array(t, n)
}
Type::Never => Type::Never,
Type::Unresolved(_) => unreachable!(),
Type::Var(_) => unreachable!(),
Type::Hole => unreachable!(),
}
}

pub fn instantiate(&self, sub: &[(Name, Type)]) -> Type {
match self {
Type::Cons(x, ts) => {
Expand Down Expand Up @@ -234,15 +272,15 @@ impl StmtTrait {
}
}

impl TraitDef {
pub fn map_type(&self, f: &impl Fn(&Type) -> Type) -> TraitDef {
impl StmtTraitDef {
pub fn map_type(&self, f: &impl Fn(&Type) -> Type) -> StmtTraitDef {
let span = self.span;
let name = self.name;
let generics = self.generics.clone();
let params = self.params.iter().map(|(x, t)| (*x, f(t))).collect();
let ty = f(&self.ty);
let where_clause = self.where_clause.iter().map(|b| b.map_type(f)).collect();
TraitDef::new(span, name, generics, params, ty, where_clause)
StmtTraitDef::new(span, name, generics, params, ty, where_clause)
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ pub struct StmtTrait {
pub name: Name,
pub generics: Vec<Name>,
pub where_clause: Vec<Bound>,
pub defs: Vec<Rc<TraitDef>>,
pub types: Vec<Rc<TraitType>>,
pub defs: Vec<Rc<StmtTraitDef>>,
pub types: Vec<Rc<StmtTraitType>>,
}

// A trait is like a predicate
Expand Down Expand Up @@ -325,14 +325,14 @@ impl Stmt {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TraitType {
pub struct StmtTraitType {
pub span: Span,
pub name: Name,
pub generics: Vec<Name>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TraitDef {
pub struct StmtTraitDef {
pub span: Span,
pub name: Name,
pub generics: Vec<Name>,
Expand Down Expand Up @@ -515,8 +515,8 @@ pub enum Pat {
Struct(Span, Type, Name, Vec<Type>, Map<Name, Pat>),
Record(Span, Type, Map<Name, Pat>),
Enum(Span, Type, Name, Vec<Type>, Name, Rc<Pat>),
Int(Span, Type, String),
String(Span, Type, String),
Int(Span, Type, Symbol),
String(Span, Type, Symbol),
Char(Span, Type, char),
Bool(Span, Type, bool),
Wildcard(Span, Type),
Expand Down Expand Up @@ -582,8 +582,8 @@ impl StmtTrait {
name: Name,
generics: Vec<Name>,
bounds: Vec<Bound>,
defs: Vec<Rc<TraitDef>>,
types: Vec<Rc<TraitType>>,
defs: Vec<Rc<StmtTraitDef>>,
types: Vec<Rc<StmtTraitType>>,
) -> StmtTrait {
StmtTrait {
span,
Expand All @@ -596,7 +596,7 @@ impl StmtTrait {
}
}

impl TraitType {
impl StmtTraitType {
pub fn new(span: Span, name: Name, generics: Vec<Name>) -> Self {
Self {
span,
Expand Down Expand Up @@ -661,7 +661,7 @@ impl StmtEnum {
}
}

impl TraitDef {
impl StmtTraitDef {
pub fn new(
span: Span,
name: Name,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/builtins/decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Compiler {
// self.declare_duration();
// self.declare_encoding();
self.declare_f32();
// self.declare_f64();
self.declare_f64();
// self.declare_file();
// self.declare_function();
// self.declare_i128();
Expand Down
24 changes: 12 additions & 12 deletions crates/compiler/src/builtins/decls/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ impl Compiler {
pub(super) fn declare_f32(&mut self) {
self.declare_type("type f32;", BuiltinType { rust: "f32" });

// self.declare_def(
// "def add_f32(a:f32,b:f32): f32;",
// BuiltinDef {
// rust: "f32::add_f32",
// fun: |_ctx, _t, v| {
// let v0 = v[0].as_f32();
// let v1 = v[1].as_f32();
// (v0 + v1).into()
// },
// },
// );
//
self.declare_def(
"def add_f32(a:f32,b:f32): f32;",
BuiltinDef {
rust: "f32::add_f32",
fun: |_ctx, _t, v| {
let v0 = v[0].as_f32();
let v1 = v[1].as_f32();
(v0 + v1).into()
},
},
);

// self.declare_def(
// "def sub_f32(a:f32,b:f32): f32;",
// BuiltinDef {
Expand Down
40 changes: 20 additions & 20 deletions crates/compiler/src/builtins/decls/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ use crate::Compiler;
impl Compiler {
pub(super) fn declare_f64(&mut self) {
self.declare_type("type f64;", BuiltinType { rust: "f64" });
self.declare_def(
"def neg_f64(a:f64): f64;",
BuiltinDef {
rust: "(|v|-v)",
fun: |_ctx, _t, v| {
let v0 = v[0].as_f64();
(-v0).into()
},
},
);
self.declare_def(
"def pos_f64(a:f64): f64;",
BuiltinDef {
rust: "(|v|v)",
fun: |_ctx, _t, v| {
let v0 = v[0].as_f64();
v0.into()
},
},
);
// self.declare_def(
// "def neg_f64(a:f64): f64;",
// BuiltinDef {
// rust: "(|v|-v)",
// fun: |_ctx, _t, v| {
// let v0 = v[0].as_f64();
// (-v0).into()
// },
// },
// );
// self.declare_def(
// "def pos_f64(a:f64): f64;",
// BuiltinDef {
// rust: "(|v|v)",
// fun: |_ctx, _t, v| {
// let v0 = v[0].as_f64();
// v0.into()
// },
// },
// );
}
}
8 changes: 4 additions & 4 deletions crates/compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::ast::StmtTrait;
use crate::ast::StmtType;
use crate::ast::StmtTypeBody;
use crate::ast::StmtVar;
use crate::ast::TraitDef;
use crate::ast::TraitType;
use crate::ast::StmtTraitDef;
use crate::ast::StmtTraitType;
use crate::ast::Type;
use crate::print::Print;

Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'a, 'b> Rust<'a, 'b> {
})
}

fn stmt_def_decl(&mut self, s: &TraitDef) -> std::fmt::Result {
fn stmt_def_decl(&mut self, s: &StmtTraitDef) -> std::fmt::Result {
self.kw("def")?;
self.space()?;
self.name(&s.name)?;
Expand All @@ -206,7 +206,7 @@ impl<'a, 'b> Rust<'a, 'b> {
self.punct(";")
}

fn stmt_type_decl(&mut self, s: &TraitType) -> std::fmt::Result {
fn stmt_type_decl(&mut self, s: &StmtTraitType) -> std::fmt::Result {
self.kw("type")?;
self.space()?;
self.name(&s.name)?;
Expand Down
Loading

0 comments on commit aa8e537

Please sign in to comment.