-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
220 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ Cargo.lock | |
*.pdb | ||
|
||
typings/ | ||
node_modules | ||
out/ | ||
client/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "enderpy" | ||
description = "A typechecker for Python" | ||
authors = ["Shaygan Hooshyari"] | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.71" | ||
clap = { version = "4.2.7", features = ["derive"] } | ||
parser = { path = "../parser" , version = "0.1.0" } | ||
typechecker = { path = "../typechecker" , version = "0.1.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::{Parser, Subcommand}; | ||
|
||
/// Enderpy CLI | ||
#[derive(Parser)] | ||
#[command(name = "Enderpy", author, version, about)] | ||
pub struct Cli { | ||
#[command(subcommand)] | ||
pub command: Commands, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Commands { | ||
/// Print lexer tokens | ||
Tokenize { | ||
/// Path to source file | ||
file: PathBuf, | ||
}, | ||
/// Print abstract syntax tree | ||
Parse { | ||
/// Path to source file | ||
file: PathBuf, | ||
}, | ||
/// Type check | ||
Check { path: PathBuf }, | ||
/// Symbol table | ||
Symbols { path: PathBuf }, | ||
|
||
/// Watch changes to type check | ||
Watch, | ||
} | ||
|
||
#[test] | ||
fn verify_cli() { | ||
use clap::CommandFactory; | ||
Cli::command().debug_assert() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use anyhow::{anyhow, Result, bail}; | ||
use clap::Parser as ClapParser; | ||
use cli::{Cli, Commands}; | ||
use parser::{token, Lexer, Parser}; | ||
use std::{fs, path::PathBuf}; | ||
use typechecker::{ | ||
build::{BuildManager, BuildSource}, | ||
settings::{ImportDiscovery, Settings}, project::find_project_root, | ||
}; | ||
|
||
mod cli; | ||
|
||
fn main() -> Result<()> { | ||
let cli = Cli::parse(); | ||
|
||
match &cli.command { | ||
Commands::Tokenize { file } => tokenize(file), | ||
Commands::Parse { file } => parse(file), | ||
Commands::Check { path } => check(path), | ||
Commands::Watch => watch(), | ||
Commands::Symbols { path } => symbols(path), | ||
} | ||
} | ||
|
||
fn symbols(path: &PathBuf) -> std::result::Result<(), anyhow::Error> { | ||
let source = fs::read_to_string(path)?; | ||
let initial_source = BuildSource { | ||
path: path.to_owned(), | ||
module: String::from("test"), | ||
source, | ||
followed: false, | ||
}; | ||
let dir_of_path = path.parent().unwrap(); | ||
let python_executable = Some( get_python_executable()? ); | ||
let settings = Settings { debug: true, root: dir_of_path.to_path_buf(), import_discovery: ImportDiscovery { python_executable } }; | ||
|
||
let mut manager = BuildManager::new(vec![initial_source], settings); | ||
manager.build(); | ||
|
||
for (name, module) in manager.modules.iter() { | ||
println!("{}", name); | ||
println!("{}", module.get_symbol_table()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_python_executable() -> Result<PathBuf> { | ||
let output = std::process::Command::new("python") | ||
.arg("-c") | ||
.arg("import sys; print(sys.executable)") | ||
.output()?; | ||
let path = String::from_utf8(output.stdout)?; | ||
Ok(PathBuf::from(path)) | ||
} | ||
|
||
fn tokenize(file: &PathBuf) -> Result<()> { | ||
let source = fs::read_to_string(file)?; | ||
let mut lexer = Lexer::new(&source); | ||
let mut tokens = Vec::new(); | ||
while let Ok(token) = lexer.next_token() { | ||
tokens.push(token.clone()); | ||
if token.kind == token::Kind::Eof { | ||
break; | ||
} | ||
} | ||
println!("{:#?}", tokens); | ||
Ok(()) | ||
} | ||
|
||
fn parse(file: &PathBuf) -> Result<()> { | ||
let source = fs::read_to_string(file)?; | ||
let mut parser = Parser::new(source); | ||
let ast = parser.parse(); | ||
println!("{:#?}", ast); | ||
Ok(()) | ||
} | ||
|
||
fn check(path: &PathBuf) -> Result<()> { | ||
if path.is_dir() { | ||
return bail!("Path must be a file"); | ||
} | ||
let source = fs::read_to_string(path)?; | ||
let initial_source = BuildSource { | ||
path: path.to_owned(), | ||
module: String::from("test"), | ||
source, | ||
followed: false, | ||
}; | ||
let root = find_project_root(path); | ||
let python_executable = Some( get_python_executable()? ); | ||
let settings = Settings { debug: true, root: PathBuf::from(root), import_discovery: ImportDiscovery { python_executable } }; | ||
let mut build_manager = BuildManager::new(vec![initial_source], settings); | ||
build_manager.type_check(); | ||
|
||
for err in build_manager.get_errors() { | ||
println!("{:#?}", err); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn watch() -> Result<()> { | ||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::path::{Path, PathBuf}; | ||
const PROJECT_ROOT_MARKERS: [&str; 2] = ["__init__.py", "pyproject.toml"]; | ||
|
||
pub fn find_project_root(path: &PathBuf) -> &Path { | ||
let root = path | ||
.ancestors() | ||
.find(|p| PROJECT_ROOT_MARKERS.iter().any(|m| p.join(m).exists())); | ||
match root { | ||
Some(root) => root, | ||
None => { | ||
if path.is_dir() { | ||
path | ||
} else { | ||
path.parent().unwrap_or(path) | ||
} | ||
} | ||
} | ||
} |