Skip to content

Commit

Permalink
Changed name of ocean_helpers to ocean-macros since I found it is an …
Browse files Browse the repository at this point in the history
…idiom with rust. Also, added default values for the New macro and added that macro a bunch of places.
  • Loading branch information
jmeaster30 committed Mar 7, 2024
1 parent 2e8d4ca commit 9354ff5
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 170 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ on:
push:
branches: [ "main" ]
paths:
- ocean_helpers/**
- ocean-macros/**
- src/**
- std/**
- examples/**
pull_request:
branches: [ "main" ]
paths:
- ocean_helpers/**
- ocean-macros/**
- src/**
- std/**
- examples/**
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ debug = true
graphviz-rust = "0.6.6"
itertools = "0.11.0"
lazy_static = "1.4.0"
ocean_helpers = { path = "ocean_helpers" }
ocean-macros = { path = "ocean-macros" }
rustyline = "12.0.0"
uuid = "1.7.0"
which = "5.0.0"
18 changes: 9 additions & 9 deletions ocean_helpers/Cargo.lock → ocean-macros/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ocean_helpers/Cargo.toml → ocean-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "ocean_helpers"
name = "ocean-macros"
version = "0.1.0"
edition = "2021"

[lib]
name = "ocean_helpers"
name = "ocean_macros"
path = "src/lib.rs"
proc-macro = true

Expand Down
22 changes: 17 additions & 5 deletions ocean_helpers/src/lib.rs → ocean-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ fn impl_ast_node_macro(ast: &syn::DeriveInput) -> TokenStream {
gen.into()
}


#[proc_macro_derive(New)]
#[proc_macro_derive(New, attributes(default))]
pub fn new_macro_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_new_macro(&ast)
Expand All @@ -65,9 +64,16 @@ fn impl_new_macro(ast: &syn::DeriveInput) -> TokenStream {
match &struct_data.fields {
Fields::Named(named_fields) => {
for field in &named_fields.named {
let default_value_attribute = field.attrs.iter().filter(|x| x.path.segments.len() == 1 && x.path.segments[0].ident == "default").nth(0);
let optional_default_value = if let Some(attr) = default_value_attribute {
Some(attr.tokens.to_token_stream())
} else {
None
};

match &field.ident {
Some(field_name) => {
typed_args.push((field_name.to_string(), field.ty.to_token_stream()))
typed_args.push((field_name.to_string(), field.ty.to_token_stream(), optional_default_value))
}
None => {}
}
Expand All @@ -77,7 +83,9 @@ fn impl_new_macro(ast: &syn::DeriveInput) -> TokenStream {
}

token_stream += "pub fn new(";
for (idx, (arg_name, arg_type)) in typed_args.iter().enumerate() {
for (idx, (arg_name, arg_type, arg_default_value)) in typed_args.iter().enumerate() {
if arg_default_value.is_some() { continue }

token_stream += arg_name.to_string().as_str();
token_stream += ": ";
token_stream += arg_type.to_string().as_str();
Expand All @@ -86,8 +94,12 @@ fn impl_new_macro(ast: &syn::DeriveInput) -> TokenStream {
}
}
token_stream += ") -> Self { Self { ";
for (idx, (arg_name, _)) in typed_args.iter().enumerate() {
for (idx, (arg_name, _, arg_default_value)) in typed_args.iter().enumerate() {
token_stream += arg_name;
if let Some(value) = arg_default_value {
token_stream += ": ";
token_stream += value.to_string().as_str();
}
if idx != typed_args.len() - 1 {
token_stream += ", "
}
Expand Down
2 changes: 1 addition & 1 deletion src/hydro/executioncontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::value::Value;

use crate::hydro::exception::Exception;
use crate::hydro::value::Reference;
use ocean_helpers::{make_add_operations, make_bit_operations, make_comparison_operations};
use ocean_macros::{make_add_operations, make_bit_operations, make_comparison_operations};
use std::collections::HashMap;
use std::ops::Deref;

Expand Down
2 changes: 1 addition & 1 deletion src/hydro/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl Parser {
fn create_default_value_from_type_string(type_lexeme: String) -> Value {
match type_lexeme.as_str() {
"bool" => Value::Boolean(false),
"string" => Value::Array(Array::new(Type::Unsigned8, Box::new(Value::Unsigned8(0)))),
"string" => Value::Array(Array::new(Type::Unsigned8, Box::new(Value::Unsigned8(0)), Vec::new())),
"u8" => Value::Unsigned8(0),
"u16" => Value::Unsigned16(0),
"u32" => Value::Unsigned32(0),
Expand Down
8 changes: 3 additions & 5 deletions src/hydro/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@ use crate::hydro::function::Target::{Index, Label};
use crate::hydro::instruction::*;
use crate::hydro::value::{Reference, Type, Value, VariableRef};
use std::collections::HashMap;
use ocean_macros::New;

#[derive(Debug, Clone)]
pub enum Target {
Label(String),
Index(usize),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, New)]
pub struct Function {
pub name: String,
pub parameters: Vec<Type>,
pub body: Vec<Instruction>,
#[default(HashMap::new())]
pub jump_labels: HashMap<String, usize>,
}

impl Function {
pub fn new(name: String, parameters: Vec<Type>, body: Vec<Instruction>) -> Self {
Self { name, parameters, body, jump_labels: HashMap::new() }
}

pub fn get_target_pointer(&self, target: Target) -> Result<usize, String> {
match target {
Label(label_name) => match self.jump_labels.get(label_name.as_str()) {
Expand Down
2 changes: 1 addition & 1 deletion src/hydro/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::value::Value;
use crate::hydro::function::Target;
use crate::hydro::value::Type;
use ocean_helpers::Debuggable;
use ocean_macros::Debuggable;
// Intellij thinks these are unused but they are used by the Debuggable derive macro
use crate::hydro::compilationunit::CompilationUnit;
use crate::hydro::debugcontext::DebugContext;
Expand Down
7 changes: 2 additions & 5 deletions src/hydro/intrinsic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use crate::hydro::value::Type;
use std::collections::HashMap;
use ocean_macros::New;

pub mod intrinsicmanager;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, New)]
pub struct Intrinsic {
pub name: String,
pub parameters: Vec<Type>,
pub target_map: HashMap<String, String>,
}

impl Intrinsic {
pub fn new(name: String, parameters: Vec<Type>, target_map: HashMap<String, String>) -> Self {
Self { name, parameters, target_map }
}

pub fn get_intrinsic_code(&self, target: String) -> Result<String, String> {
match self.target_map.get(target.as_str()) {
None => Err(format!("Could not find code for target {}", target)),
Expand Down
7 changes: 2 additions & 5 deletions src/hydro/layouttemplate.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use crate::hydro::value::{Layout, Type, Value};
use std::collections::HashMap;
use ocean_macros::New;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, New)]
pub struct LayoutTemplate {
pub name: String,
pub members: HashMap<String, Value>,
}

impl LayoutTemplate {
pub fn new(name: String, members: HashMap<String, Value>) -> Self {
Self { name, members }
}

pub fn build(name: &str) -> Self {
Self { name: name.to_string(), members: HashMap::new() }
}
Expand Down
55 changes: 8 additions & 47 deletions src/hydro/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use ocean_macros::New;

#[derive(Debug, Clone, PartialEq)]
pub enum Type {
Expand Down Expand Up @@ -143,7 +144,7 @@ pub enum Value {
impl Value {
pub fn string(value: String) -> Value {
let bytes = value.clone().into_bytes().iter().map(|x| Value::Unsigned8(*x)).collect::<Vec<Value>>();
Value::Array(Array::create(Type::Unsigned8, Box::new(Value::Unsigned64(value.len() as u64)), bytes))
Value::Array(Array::new(Type::Unsigned8, Box::new(Value::Unsigned64(value.len() as u64)), bytes))
}

pub fn type_of(&self) -> Type {
Expand Down Expand Up @@ -330,90 +331,50 @@ impl Value {
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, New)]
pub struct FunctionPointer {
pub module: Option<String>,
pub function: String,
}

impl FunctionPointer {
pub fn new(module: Option<String>, function: String) -> Self {
Self { module, function }
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Reference {
Variable(VariableRef),
ArrayIndex(ArrayIndexRef),
LayoutIndex(LayoutIndexRef),
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, New)]
pub struct VariableRef {
pub name: String,
}

impl VariableRef {
pub fn new(name: String) -> Self {
Self { name }
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, New)]
pub struct ArrayIndexRef {
pub reference: Box<Value>,
pub index: Box<Value>,
}

impl ArrayIndexRef {
pub fn new(reference: Box<Value>, index: Box<Value>) -> Self {
Self { reference, index }
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, New)]
pub struct LayoutIndexRef {
pub reference: Box<Value>,
pub index: String,
}

impl LayoutIndexRef {
pub fn new(reference: Box<Value>, index: String) -> Self {
Self { reference, index }
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Clone, PartialEq, PartialOrd, New)]
pub struct Array {
pub value_type: Type,
pub length: Box<Value>,
pub values: Vec<Value>,
}

impl Array {
pub fn new(value_type: Type, length: Box<Value>) -> Self {
Self { value_type, length, values: Vec::new() }
}

pub fn create(value_type: Type, length: Box<Value>, values: Vec<Value>) -> Self {
Self { value_type, length, values }
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, New)]
pub struct Layout {
pub module_name: String,
pub layout_name: String,
pub values: HashMap<String, Value>,
}

impl Layout {
pub fn new(module_name: String, layout_name: String, values: HashMap<String, Value>) -> Self {
Self { module_name, layout_name, values }
}
}

impl PartialOrd for Layout {
fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
todo!()
Expand Down
Loading

0 comments on commit 9354ff5

Please sign in to comment.