Skip to content

Commit

Permalink
Creating Composite type structure and parsing generics from Python
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Oct 31, 2023
1 parent e88414b commit ecad271
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 47 deletions.
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/function/full_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl FullParser {

fn parse_output(&self, output: Option<Box<Expr>>) -> Result<Option<Type>> {
if let Some(expr) = output.and_then(|expr| expr.name_expr()) {
Ok(Some(TypeParser.parse(expr)?))
Ok(Some(TypeParser.parse(&expr)?))
} else {
Ok(None)
}
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/python/parser/src/function/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Parser<ArgWithDefault> for ParameterParser {
let attributes = Default::default();
let identifier = IdentifierParser::new().parse(input.def.arg.as_str())?;
let type_ = if let Some(value) = input.def.annotation.and_then(|annotation| annotation.name_expr()) {
TypeParser.parse(value)?
TypeParser.parse(&value)?
} else {
Default::default()
};
Expand Down
5 changes: 4 additions & 1 deletion ecosystem/python/parser/src/object/full_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
use ligen::ir::Object;
use crate::identifier::IdentifierParser;
use crate::prelude::*;
use crate::types::type_::TypeParser;

use super::DynamicParser;

Expand All @@ -13,7 +14,9 @@ pub struct FullParser;
impl Parser<&StmtAnnAssign> for FullParser {
type Output = Object;
fn parse(&self, input: &StmtAnnAssign) -> Result<Self::Output> {
self.parse(input.target.as_ref())
let mut object = self.parse(input.target.as_ref())?;
object.type_ = TypeParser::new().parse(&*input.annotation)?;
Ok(object)
}
}

Expand Down
44 changes: 37 additions & 7 deletions ecosystem/python/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustpython_parser::ast::ExprName;
use ligen::ir::{Type, Primitive, Integer, Float};
use rustpython_parser::ast::{ExprName, Expr, ExprSubscript, ExprTuple};
use ligen::ir::{Type, Primitive, Integer, Float, Identifier};
use crate::prelude::*;

#[derive(Default)]
Expand All @@ -11,17 +11,47 @@ impl TypeParser {
}
}

impl Parser<ExprName> for TypeParser {
impl Parser<&ExprName> for TypeParser {
type Output = Type;
fn parse(&self, input: ExprName) -> Result<Self::Output> {
let name = input.id;
match name.as_str() {
fn parse(&self, input: &ExprName) -> Result<Self::Output> {
match input.id.as_str() {
"bool" => Ok(Primitive::Boolean.into()),
"char" => Ok(Primitive::Character.into()),
"byte" => Ok(Integer::I8.into()),
"int" => Ok(Integer::I32.into()),
"float" => Ok(Float::F32.into()),
name => Ok(Type::Composite(name.into(), Default::default()))
name => Ok(Type::Composite(Identifier::from(name).into()))
}
}
}

impl Parser<&ExprSubscript> for TypeParser {
type Output = Type;
fn parse(&self, input: &ExprSubscript) -> Result<Self::Output> {
let mut type_ = self.parse(&*input.value)?;
if let Type::Composite(composite) = &mut type_ {
let type_ = self.parse(&*input.slice)?;
composite.generics.types.push(type_);
}
Ok(type_)
}
}

impl Parser<&ExprTuple> for TypeParser {
type Output = Type;
fn parse(&self, _input: &ExprTuple) -> Result<Self::Output> {
todo!("Tuple not implemented yet");
}
}

impl Parser<&Expr> for TypeParser {
type Output = Type;
fn parse(&self, input: &Expr) -> Result<Self::Output> {
match input {
Expr::Name(expr) => self.parse(expr),
Expr::Subscript(expr) => self.parse(expr),
Expr::Tuple(expr) => self.parse(expr),
_ => Err(Error::Message("Expected type".into()))
}
}
}
6 changes: 4 additions & 2 deletions ecosystem/rust/parser/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ligen::ir::{Primitive, Reference, Mutability, Type};
use ligen::ir::{Primitive, Reference, Mutability, Type, Composite};
use crate::prelude::*;
use ligen::parsing::parser::Parser;
use crate::path::PathParser;
Expand All @@ -18,7 +18,9 @@ impl Parser<syn::Path> for TypeParser {
.last()
.map(|segment| GenericsParser.parse(segment.arguments.clone()).expect("Failed to parse generics."))
.unwrap_or_default();
Ok(Self::Output::Composite(PathParser.parse(path)?, generics))
let path = PathParser.parse(path)?;
let composite = Composite { path, generics };
Ok(Self::Output::Composite(composite))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions ligen/ir/src/function/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn function_output() -> Function {
synchrony: Synchrony::Synchronous,
identifier: "test".into(),
inputs: vec![],
output: Some(Type::Composite(Identifier::new("String").into(), Default::default()))
output: Some(Type::Composite(Identifier::new("String").into()))
}
}

Expand Down Expand Up @@ -118,28 +118,28 @@ pub fn function_complete() -> Function {
Parameter {
attributes: Default::default(),
identifier: Identifier::new("a"),
type_: Type::Composite(Identifier::new("String").into(), Default::default())
type_: Type::Composite(Identifier::new("String").into())
},
Parameter {
attributes: Default::default(),
identifier: Identifier::new("b"),
type_: Type::Reference(Reference {
mutability: Mutability::Constant,
type_: Box::new(Type::Composite(Identifier::new("String").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("String").into()))
})
},
Parameter {
attributes: Default::default(),
identifier: Identifier::new("c"),
type_: Type::Reference(Reference {
mutability: Mutability::Mutable,
type_: Box::new(Type::Composite(Identifier::new("String").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("String").into()))
})
},
],
output: Some(Type::Reference(Reference {
mutability: Mutability::Constant,
type_: Box::new(Type::Composite(Identifier::new("String").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("String").into()))
}))
}
}
12 changes: 6 additions & 6 deletions ligen/ir/src/function/parameter/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn parameter_attribute() -> Parameter {
pub fn composite_parameter() -> Parameter {
Parameter {
identifier: Identifier::new("name"),
type_: Type::Composite(Identifier::new("String").into(), Default::default()),
type_: Type::Composite(Identifier::new("String").into()),
.. Default::default()
}
}
Expand All @@ -30,7 +30,7 @@ pub fn constant_reference_parameter() -> Parameter {
type_: Type::Reference(
Reference {
mutability: Mutability::Constant,
type_: Box::new(Type::Composite(Identifier::new("String").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("String").into()))
}
),
.. Default::default()
Expand All @@ -43,7 +43,7 @@ pub fn mutable_reference_parameter() -> Parameter {
type_: Type::Reference(
Reference {
mutability: Mutability::Mutable,
type_: Box::new(Type::Composite(Identifier::new("String").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("String").into()))
}
),
.. Default::default()
Expand All @@ -53,7 +53,7 @@ pub fn mutable_reference_parameter() -> Parameter {
pub fn receiver_parameter() -> Parameter {
Parameter {
identifier: Identifier::new("self"),
type_: Type::Composite(Identifier::new("Self").into(), Default::default()),
type_: Type::Composite(Identifier::new("Self").into()),
.. Default::default()
}
}
Expand All @@ -64,7 +64,7 @@ pub fn reference_receiver_parameter() -> Parameter {
type_: Type::Reference(
Reference {
mutability: Mutability::Constant,
type_: Box::new(Type::Composite(Identifier::new("Self").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("Self").into()))
}
),
.. Default::default()
Expand All @@ -77,7 +77,7 @@ pub fn mutable_receiver_parameter() -> Parameter {
type_: Type::Reference(
Reference {
mutability: Mutability::Mutable,
type_: Box::new(Type::Composite(Identifier::new("Self").into(), Default::default()))
type_: Box::new(Type::Composite(Identifier::new("Self").into()))
}
),
.. Default::default()
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions ligen/ir/src/types/composite/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::{prelude::*, Path, Identifier};

pub mod generics;

pub use generics::*;

#[derive(Default, Debug, Hash, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Composite {
pub path: Path,
pub generics: Generics
}

impl From<Identifier> for Composite {
fn from(value: Identifier) -> Self {
Path::from(value).into()
}
}

impl From<Path> for Composite {
fn from(path: Path) -> Self {
let generics = Default::default();
Self { path, generics }
}
}

impl std::fmt::Display for Composite {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(&format!("{}{}", self.path, self.generics))
}
}
4 changes: 2 additions & 2 deletions ligen/ir/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ pub mod type_definition;
pub mod type_;
pub mod reference;
pub mod primitive;
pub mod generics;
pub mod composite;

pub use type_definition::{TypeDefinition, KindDefinition, Enumeration, Structure, Field, Variant, structure, enumeration};
pub use type_::*;
pub use reference::*;
pub use primitive::*;
pub use generics::*;
pub use composite::*;
12 changes: 6 additions & 6 deletions ligen/ir/src/types/type_.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Primitive, Reference, Path, Identifier, Integer, Float, Generics};
use crate::{Primitive, Reference, Path, Identifier, Integer, Float, Composite};
use crate::prelude::*;
use std::ops::Deref;

Expand All @@ -8,7 +8,7 @@ pub enum Type {
/// Primitive variant
Primitive(Primitive),
/// Composite variant
Composite(Path, Generics),
Composite(Composite),
/// Reference variant
Reference(Reference),
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Type {
/// Check if the `Type` is `String`.
pub fn is_string(&self) -> bool {
match self {
Self::Composite(path, _) => path == &Path::from("String"), // TODO: Create a String type.
Self::Composite(composite) => composite.path == Path::from("String"), // TODO: Create a String type.
_ => false
}
}
Expand All @@ -84,13 +84,13 @@ impl Type {

impl From<Identifier> for Type {
fn from(identifier: Identifier) -> Self {
Self::Composite(identifier.into(), Default::default())
Self::Composite(identifier.into())
}
}

impl From<Path> for Type {
fn from(path: Path) -> Self {
Self::Composite(path, Default::default())
Self::Composite(path.into())
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let display = match &self {
Type::Primitive(primitive) => format!("{}", primitive),
Type::Composite(composite, generics) => format!("{}{}", composite, generics),
Type::Composite(composite) => format!("{}", composite),
Type::Reference(reference) => format!("{}", reference),
};
f.write_str(&display)
Expand Down
1 change: 1 addition & 0 deletions ligen/parsing/src/parser/universal/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl Parser<String> for IdentifierParser {
impl Parser<&str> for IdentifierParser {
type Output = Identifier;
fn parse(&self, input: &str) -> Result<Self::Output> {
// TODO: check if ident is valid identifier.
let name = input.into();
Ok(Identifier { name })
}
Expand Down
6 changes: 3 additions & 3 deletions ligen/utils/src/transformers/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl Transform<FunctionVisitor, Function> for RelativePathToAbsolutePath {

fn type_to_absolute_path(module_visitor: &ModuleVisitor, type_: &mut Type) {
match type_ {
Type::Composite(path, _) => {
if let Some(absolute_path) = module_visitor.find_absolute_path(path) {
*path = absolute_path
Type::Composite(composite) => {
if let Some(absolute_path) = module_visitor.find_absolute_path(&composite.path) {
composite.path = absolute_path
}
},
Type::Reference(reference) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ impl Generics {
impl Widget for Generics {
type Input = ligen_ir::Generics;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, generics: &mut ligen_ir::Generics) {
EditableList::new(generics.to_string(), "Add type").show(settings, ui, &mut generics.types, |ui, type_| {
Type::new().show(settings, ui, type_);
});
if settings.editor.editable_fields {
EditableList::new(generics.to_string(), "Add type").show(settings, ui, &mut generics.types, |ui, type_| {
Type::new().show(settings, ui, type_);
});
}
}
}
28 changes: 28 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/ir/type_/composite/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod generics;

use crate::gui::ui::editor::{widget::Widget, ir::Path};

pub use generics::*;

#[derive(Default)]
pub struct Composite {}

impl Composite {
pub fn new() -> Self {
Default::default()
}
}

impl Widget for Composite {
type Input = ligen_ir::Composite;
fn show(&mut self, settings: &crate::gui::ui::editor::settings::Settings, ui: &mut ligen_gui_runtime::egui::Ui, input: &mut Self::Input) {
if settings.editor.editable_fields {
ui.horizontal_top(|ui| {
Path::new().show(settings, ui, &mut input.path);
Generics::new().show(settings, ui, &mut input.generics);
});
} else {
ui.label(input.to_string());
}
}
}
Loading

0 comments on commit ecad271

Please sign in to comment.