Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Compile tips + fix types #33

Merged
merged 11 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,283 changes: 2,734 additions & 549 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2022 the Whistle authors
Copyright (c) 2020-2023 the Whistle authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion ast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whistle_ast"
version = "0.1.0"
version = "0.1.1"
authors = ["the Whistle authors"]
edition = "2021"

Expand Down
5 changes: 5 additions & 0 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ pub enum ProgramStmt {
stmt: Stmt,
span: Span,
},
Tip {
tip: Tip,
span: Span,
},
}

impl ProgramStmt {
Expand All @@ -355,6 +359,7 @@ impl ProgramStmt {
ProgramStmt::StructDecl { span, .. } => span.clone(),
ProgramStmt::TypeDecl { span, .. } => span.clone(),
ProgramStmt::Stmt { span, .. } => span.clone(),
ProgramStmt::Tip { span, .. } => span.clone(),
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
[package]
name = "whistle_cli"
version = "0.1.0"
version = "0.1.1"
authors = ["the Whistle authors"]
edition = "2021"

[[bin]]
name = "whistle"
path = "src/main.rs"


[dependencies]
whistle_ast = { path = "../ast/" }
whistle_common = { path = "../common/" }
whistle_compiler = { path = "../compiler/" }
whistle_lexer = { path = "../lexer/" }
whistle_parser = { path = "../parser/" }
whistle_lexer = { path = "../lexer" }
whistle_parser = { path = "../parser" }
whistle_common = { path = "../common" }
whistle_ast = { path = "../ast" }
whistle_compiler = { path = "../compiler" }
whistle_preprocessor = { path = "../preprocessor" }
clap = "2.33.3"
clap = { version = "4.0.29", features = ["derive"] }
wasmprinter = "0.2.45"
wasmtime = "12.0.2"
wasmtime-wasi = "12.0.2"
tokio = { version = "1.23.0", features = ["full"] }
tokio-util = { version = "0.7", features = ["compat"] }
tower-lsp = { version = "0.20.0"}
toml = "0.7.2"
tracing-subscriber = "0.3"
serde = { version = "1.0.149", features = ["derive"] }
serde_json = "1.0.89"
futures = "0.3.25"
ropey = "1.6.0"
uuid = { version = "1.2.2", features = ["v4", "fast-rng", "macro-diagnostics"] }
3 changes: 3 additions & 0 deletions cli/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod lsp;

pub use lsp::*;
163 changes: 163 additions & 0 deletions cli/src/lsp/lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use ropey::Rope;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};

#[derive(Debug)]
pub struct Document {
pub language_id: String,
pub text: Rope,
}

impl Document {
pub fn new(language_id: String, text: Rope) -> Self {
Self { language_id, text }
}
}
#[derive(Debug)]
pub struct WhistleBackend {
pub client: Client,
pub document_map: Arc<RwLock<HashMap<String, Document>>>,
}

#[tower_lsp::async_trait]
impl LanguageServer for WhistleBackend {
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
completion_provider: Some(CompletionOptions::default()),
..Default::default()
},
..Default::default()
})
}

async fn initialized(&self, _: InitializedParams) {
self.client
.log_message(MessageType::INFO, "Whistle language server initialized!")
.await;
}

async fn shutdown(&self) -> Result<()> {
Ok(())
}

// async fn did_change_workspace_folders(&self, _: DidChangeWorkspaceFoldersParams) {
// self.client
// .log_message(MessageType::INFO, "workspace folders changed!")
// .await;
// }

// async fn did_change_configuration(&self, _: DidChangeConfigurationParams) {
// self.client
// .log_message(MessageType::INFO, "configuration changed!")
// .await;
// }

// async fn did_change_watched_files(&self, _: DidChangeWatchedFilesParams) {
// self.client
// .log_message(MessageType::INFO, "watched files have changed!")
// .await;
// }
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let rope = ropey::Rope::from_str(&params.text_document.text);
let uri = params.text_document.uri.to_string();
*self
.document_map
.write()
.await
.entry(uri.clone())
.or_insert(Document::new("unknown".to_owned(), Rope::new())) =
Document::new(params.text_document.language_id, rope);
}

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let rope = ropey::Rope::from_str(&params.content_changes[0].text);
let uri = params.text_document.uri.to_string();
let mut document_map = self.document_map.write().await;
let doc = document_map
.entry(uri.clone())
.or_insert(Document::new("unknown".to_owned(), Rope::new()));
doc.text = rope;
self.client
.log_message(MessageType::INFO, format!("{}", doc.text.to_string()))
.await;
}

// async fn did_save(&self, _: DidSaveTextDocumentParams) {
// self.client
// .log_message(MessageType::INFO, "file saved!")
// .await;
// }

// async fn did_close(&self, _: DidCloseTextDocumentParams) {
// self.client
// .log_message(MessageType::INFO, "file closed!")
// .await;
// }
async fn completion(&self, _: CompletionParams) -> Result<Option<CompletionResponse>> {
Ok(Some(CompletionResponse::Array(vec![
//types
CompletionItem::new_simple("i32".to_string(), "32-bit signed integer type".to_string()),
CompletionItem::new_simple("i64".to_string(), "64-bit signed integer type".to_string()),
CompletionItem::new_simple(
"u32".to_string(),
"32-bit unsigned integer type".to_string(),
),
CompletionItem::new_simple(
"u64".to_string(),
"64-bit unsigned integer type".to_string(),
),
CompletionItem::new_simple("f32".to_string(), "32-bit floating point type".to_string()),
CompletionItem::new_simple("f64".to_string(), "64-bit floating point type".to_string()),
CompletionItem::new_simple("str".to_string(), "string".to_string()),
CompletionItem::new_simple("char".to_string(), "single character".to_string()),
CompletionItem::new_simple("bool".to_string(), "boolean".to_string()),
CompletionItem::new_simple("none".to_string(), "no value".to_string()),
CompletionItem::new_simple("number".to_string(), "number type".to_string()),
CompletionItem::new_simple("int".to_string(), "int type".to_string()),
//keywords
CompletionItem::new_simple("import".to_string(), "import declaration".to_string()),
CompletionItem::new_simple("builtin".to_string(), "builtin declaration".to_string()),
CompletionItem::new_simple("fn".to_string(), "declares a function".to_string()),
CompletionItem::new_simple("export".to_string(), "export declaration".to_string()),
CompletionItem::new_simple("return".to_string(), "return statement".to_string()),
CompletionItem::new_simple("if".to_string(), "if statement".to_string()),
CompletionItem::new_simple("else".to_string(), "else statement".to_string()),
CompletionItem::new_simple("while".to_string(), "while statement".to_string()),
CompletionItem::new_simple("break".to_string(), "break statement".to_string()),
CompletionItem::new_simple("continue".to_string(), "continue statement".to_string()),
CompletionItem::new_simple(
"var".to_string(),
"declares a function-scoped or globally-scoped variable".to_string(),
),
CompletionItem::new_simple(
"val".to_string(),
"declares a constant variable".to_string(),
),
])))
}

async fn hover(&self, _: HoverParams) -> Result<Option<Hover>> {
// let _markdown = MarkupContent {
// kind: MarkupKind::Markdown,
// value: [
// "# Header",
// "Some text",
// ]
// .join("\n"),
// };
Ok(Some(Hover {
contents: HoverContents::Scalar(MarkedString::String("".to_string())),
/// HoverContents::Markup(markdown),
range: None,
}))
}
}
Loading