This commit is contained in:
mb 2022-08-29 16:52:55 +03:00
commit 5c1cff92c6
15 changed files with 844 additions and 0 deletions

9
lorgn_lang/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "lorgn_lang"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"]}

74
lorgn_lang/src/ast.rs Normal file
View file

@ -0,0 +1,74 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct Path {
pub module: Name,
pub item: Name,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct Name(pub String);
impl From<String> for Name {
fn from(input: String) -> Self {
Self(input)
}
}
impl From<Name> for String {
fn from(input: Name) -> Self {
let Name(result) = input;
result
}
}
impl<'s> From<&'s str> for Name {
fn from(input: &'s str) -> Self {
input.to_string().into()
}
}
pub struct Import {
pub module_name: Name,
pub items: Vec<Name>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Export {
pub items: Vec<Name>,
}
pub use expression::*;
mod expression;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FnDef {
pub name: Name,
pub parameters: Vec<Name>,
pub expressions: Block,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TopLevel {
Export(Export),
FnDef(FnDef),
}
impl TopLevel {
pub fn as_fndef(&self) -> Option<&FnDef> {
match self {
Self::FnDef(result) => Some(result),
_ => None,
}
}
pub fn into_fndef(self) -> Option<FnDef> {
match self {
Self::FnDef(result) => Some(result),
_ => None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Module {
pub items: Vec<TopLevel>,
}

View file

@ -0,0 +1,135 @@
use serde::{Deserialize, Serialize};
use super::{Name, Path};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
pub expressions: Vec<BExpr>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Assignment {
pub variable_name: Name,
pub value: BExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Invoke {
pub variable_name: Name,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Litteral {
String(String),
Integer(i32),
Float(f32),
Bool(bool),
List(Vec<BExpr>),
Map(Vec<(Name, BExpr)>),
}
impl From<String> for Litteral {
fn from(input: String) -> Self {
Self::String(input)
}
}
impl From<&str> for Litteral {
fn from(input: &str) -> Self {
Self::String(input.to_string())
}
}
impl From<i32> for Litteral {
fn from(input: i32) -> Self {
Self::Integer(input)
}
}
impl From<f32> for Litteral {
fn from(input: f32) -> Self {
Self::Float(input)
}
}
impl From<bool> for Litteral {
fn from(bool: bool) -> Self {
Self::Bool(bool)
}
}
impl<T> From<Vec<T>> for Litteral
where
T: Into<Litteral>,
{
fn from(input: Vec<T>) -> Self {
let list = input
.into_iter()
.map(|e| Box::new(Expr::Litteral(e.into())))
.collect();
Self::List(list)
}
}
impl<S, T> From<Vec<(S, T)>> for Litteral
where
S: ToString,
T: Into<Litteral>,
{
fn from(input: Vec<(S, T)>) -> Self {
let list = input
.into_iter()
.map(|(n, e)| (n.to_string().into(), Box::new(Expr::Litteral(e.into()))))
.collect();
Self::Map(list)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FnCall {
pub fn_path: Path,
pub arguments: Vec<BExpr>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Condition {
pub condition: BExpr,
pub true_case: BExpr,
pub false_case: BExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Loop {
pub body: BExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Return {
pub expression: BExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Break {
pub expression: BExpr,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Expr {
Block(Block),
Assignment(Assignment),
Invoke(Invoke),
Litteral(Litteral),
FnCall(FnCall),
Condition(Condition),
Loop(Loop),
Return(Return),
Break(Break),
}
impl Expr {
pub fn boxed(self) -> BExpr {
Box::new(self)
}
}
pub type BExpr = Box<Expr>;

2
lorgn_lang/src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod ast;
pub mod typing;

6
lorgn_lang/src/typing.rs Normal file
View file

@ -0,0 +1,6 @@
pub enum Primitive {
String(String),
Integer(i32),
Float(f32),
Bool(bool),
}