chore: initialize NexaCore compiler workspace with basic frontend and CLI
Add initial project structure for NexaCore programming language compiler: - Create Cargo workspace with 4 crates (cli, driver, frontend, runtime) - Add lexer with indentation-based tokenization and keyword support - Add parser for modules, functions, structs, and basic expressions - Implement CLI with build command and placeholder subcommands - Add driver crate to orchestrate compilation pipeline - Include .gitignore for Rust build
This commit is contained in:
126
crates/nxc-frontend/src/token.rs
Normal file
126
crates/nxc-frontend/src/token.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct Span {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub fn new(start: usize, end: usize, line: usize, column: usize) -> Self {
|
||||
Self {
|
||||
start,
|
||||
end,
|
||||
line,
|
||||
column,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Token {
|
||||
pub kind: TokenKind,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn new(kind: TokenKind, span: Span) -> Self {
|
||||
Self { kind, span }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TokenKind {
|
||||
Identifier(String),
|
||||
Integer(String),
|
||||
String(String),
|
||||
Keyword(Keyword),
|
||||
LeftParen,
|
||||
RightParen,
|
||||
LeftBrace,
|
||||
RightBrace,
|
||||
LeftBracket,
|
||||
RightBracket,
|
||||
Comma,
|
||||
Dot,
|
||||
Colon,
|
||||
Arrow,
|
||||
FatArrow,
|
||||
Plus,
|
||||
Minus,
|
||||
Star,
|
||||
Slash,
|
||||
Percent,
|
||||
Equal,
|
||||
EqualEqual,
|
||||
Bang,
|
||||
BangEqual,
|
||||
Less,
|
||||
LessEqual,
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
Question,
|
||||
Newline,
|
||||
Indent,
|
||||
Dedent,
|
||||
Eof,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Keyword {
|
||||
Async,
|
||||
Else,
|
||||
Fn,
|
||||
For,
|
||||
If,
|
||||
Impl,
|
||||
Import,
|
||||
In,
|
||||
Let,
|
||||
Match,
|
||||
Pub,
|
||||
Return,
|
||||
Struct,
|
||||
Use,
|
||||
Var,
|
||||
While,
|
||||
}
|
||||
|
||||
impl Keyword {
|
||||
pub fn from_ident(value: &str) -> Option<Self> {
|
||||
match value {
|
||||
"async" => Some(Self::Async),
|
||||
"else" => Some(Self::Else),
|
||||
"fn" => Some(Self::Fn),
|
||||
"for" => Some(Self::For),
|
||||
"if" => Some(Self::If),
|
||||
"impl" => Some(Self::Impl),
|
||||
"import" => Some(Self::Import),
|
||||
"in" => Some(Self::In),
|
||||
"let" => Some(Self::Let),
|
||||
"match" => Some(Self::Match),
|
||||
"pub" => Some(Self::Pub),
|
||||
"return" => Some(Self::Return),
|
||||
"struct" => Some(Self::Struct),
|
||||
"use" => Some(Self::Use),
|
||||
"var" => Some(Self::Var),
|
||||
"while" => Some(Self::While),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TokenKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
TokenKind::Identifier(name) => write!(f, "identifier({name})"),
|
||||
TokenKind::Integer(value) => write!(f, "integer({value})"),
|
||||
TokenKind::String(value) => write!(f, "string({value})"),
|
||||
TokenKind::Keyword(keyword) => write!(f, "keyword({keyword:?})"),
|
||||
other => write!(f, "{other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user