Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hohav committed Oct 29, 2023
1 parent 8617575 commit 082a46a
Show file tree
Hide file tree
Showing 23 changed files with 1,241 additions and 1,083 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Cargo.lock
**/*.rs.bk
gen/.cpcache
gen/target
105 changes: 27 additions & 78 deletions gen/resources/preamble/frame.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#![allow(unused_parens)]
#![allow(unused_variables)]
#![allow(dead_code)]

use std::{
fmt,
io::{Result, Write},
};

use byteorder::WriteBytesExt;
use arrow2::{
array::{ListArray, MutablePrimitiveArray, PrimitiveArray, StructArray},
array::{ListArray, PrimitiveArray, StructArray},
buffer::Buffer,
datatypes::{DataType, Field},
offset::{Offsets, OffsetsBuffer},
offset::OffsetsBuffer,
};
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Result, Write};

use crate::{
model::{
columnar,
game::{Port, NUM_PORTS},
slippi::Version,
},
serde::de::Event,
};

pub mod mutable;
pub mod transpose;

type BE = byteorder::BigEndian;

/// Frame indexes start at -123, and reach 0 at "Go!".
Expand All @@ -31,6 +35,7 @@ pub struct PortOccupancy {
pub follower: bool,
}

#[derive(Debug)]
pub struct Data {
pub pre: Pre,
pub post: Post,
Expand Down Expand Up @@ -113,34 +118,16 @@ impl Data {
}
}

pub struct MutableData {
pub pre: MutablePre,
pub post: MutablePost,
}

impl MutableData {
pub fn with_capacity(capacity: usize, version: Version) -> Self {
Self {
pre: MutablePre::with_capacity(capacity, version),
post: MutablePost::with_capacity(capacity, version),
}
}

pub fn push_none(&mut self, version: Version) {
self.pre.push_none(version);
self.post.push_none(version);
}
}

impl From<MutableData> for Data {
fn from(d: MutableData) -> Self {
impl From<mutable::Data> for Data {
fn from(d: mutable::Data) -> Self {
Self {
pre: d.pre.into(),
post: d.post.into(),
}
}
}

#[derive(Debug)]
pub struct PortData {
pub port: Port,
pub leader: Data,
Expand Down Expand Up @@ -271,27 +258,8 @@ impl PortData {

}

pub struct MutablePortData {
pub port: Port,
pub leader: MutableData,
pub follower: Option<MutableData>,
}

impl MutablePortData {
pub fn with_capacity(capacity: usize, version: Version, port: PortOccupancy) -> Self {
Self {
port: port.port,
leader: MutableData::with_capacity(capacity, version),
follower: match port.follower {
true => Some(MutableData::with_capacity(capacity, version)),
_ => None,
},
}
}
}

impl From<MutablePortData> for PortData {
fn from(p: MutablePortData) -> Self {
impl From<mutable::PortData> for PortData {
fn from(p: mutable::PortData) -> Self {
Self {
port: p.port,
leader: p.leader.into(),
Expand Down Expand Up @@ -465,8 +433,8 @@ impl Frame {
Ok(())
}

pub fn transpose_one(&self, i: usize, version: Version) -> columnar::Frame {
columnar::Frame {
pub fn transpose_one(&self, i: usize, version: Version) -> transpose::Frame {
transpose::Frame {
start: self.start.transpose_one(i, version),
end: self.end.transpose_one(i, version),
}
Expand Down Expand Up @@ -496,33 +464,8 @@ impl Frame {
}
}

pub struct MutableFrame {
pub id: MutablePrimitiveArray<i32>,
pub start: MutableStart,
pub end: MutableEnd,
pub port: Vec<MutablePortData>,
pub item_offset: Offsets<i32>,
pub item: MutableItem,
}

impl MutableFrame {
pub fn with_capacity(capacity: usize, version: Version, ports: &[PortOccupancy]) -> Self {
Self {
id: MutablePrimitiveArray::<i32>::with_capacity(capacity),
start: MutableStart::with_capacity(capacity, version),
end: MutableEnd::with_capacity(capacity, version),
port: ports
.iter()
.map(|p| MutablePortData::with_capacity(capacity, version, *p))
.collect(),
item_offset: Offsets::<i32>::with_capacity(capacity),
item: MutableItem::with_capacity(0, version),
}
}
}

impl From<MutableFrame> for Frame {
fn from(f: MutableFrame) -> Self {
impl From<mutable::Frame> for Frame {
fn from(f: mutable::Frame) -> Self {
Self {
id: f.id.into(),
start: f.start.into(),
Expand All @@ -533,3 +476,9 @@ impl From<MutableFrame> for Frame {
}
}
}

impl fmt::Debug for Frame {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
write!(f, "Frame {{ len: {} }}", self.id.len())
}
}
84 changes: 84 additions & 0 deletions gen/resources/preamble/frame/mutable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![allow(unused_parens)]
#![allow(unused_variables)]
#![allow(dead_code)]

use arrow2::{
array::MutablePrimitiveArray,
offset::Offsets,
};

use byteorder::ReadBytesExt;
use std::io::Result;

use crate::{
model::{
frame::PortOccupancy,
game::Port,
slippi::Version,
},
};

type BE = byteorder::BigEndian;

pub struct Data {
pub pre: Pre,
pub post: Post,
}

impl Data {
pub fn with_capacity(capacity: usize, version: Version) -> Self {
Self {
pre: Pre::with_capacity(capacity, version),
post: Post::with_capacity(capacity, version),
}
}

pub fn push_none(&mut self, version: Version) {
self.pre.push_none(version);
self.post.push_none(version);
}
}

pub struct PortData {
pub port: Port,
pub leader: Data,
pub follower: Option<Data>,
}

impl PortData {
pub fn with_capacity(capacity: usize, version: Version, port: PortOccupancy) -> Self {
Self {
port: port.port,
leader: Data::with_capacity(capacity, version),
follower: match port.follower {
true => Some(Data::with_capacity(capacity, version)),
_ => None,
},
}
}
}

pub struct Frame {
pub id: MutablePrimitiveArray<i32>,
pub start: Start,
pub end: End,
pub port: Vec<PortData>,
pub item_offset: Offsets<i32>,
pub item: Item,
}

impl Frame {
pub fn with_capacity(capacity: usize, version: Version, ports: &[PortOccupancy]) -> Self {
Self {
id: MutablePrimitiveArray::<i32>::with_capacity(capacity),
start: Start::with_capacity(capacity, version),
end: End::with_capacity(capacity, version),
port: ports
.iter()
.map(|p| PortData::with_capacity(capacity, version, *p))
.collect(),
item_offset: Offsets::<i32>::with_capacity(capacity),
item: Item::with_capacity(0, version),
}
}
}
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions gen/scripts/columnar

This file was deleted.

2 changes: 1 addition & 1 deletion gen/scripts/frame
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
set -euo pipefail

cd "$(dirname "$0")/.."
clj -M -m peppi-codegen.frame resources/frame.json
clj -M -m peppi-codegen.frame resources/structs.json
5 changes: 5 additions & 0 deletions gen/scripts/frame-mutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")/.."
clj -M -m peppi-codegen.frame.mutable resources/structs.json
5 changes: 5 additions & 0 deletions gen/scripts/frame-transpose
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")/.."
clj -M -m peppi-codegen.frame.transpose resources/structs.json
11 changes: 7 additions & 4 deletions gen/scripts/regen
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ set -euo pipefail

cd "$(dirname "$0")/.."

scripts/frame | rustfmt > resources/frame.rs
mv resources/frame.rs ../src/model/
mkdir -p target/frame

scripts/columnar | rustfmt > resources/columnar.rs
mv resources/columnar.rs ../src/model/
scripts/frame | rustfmt > target/frame.rs
scripts/frame-mutable | rustfmt > target/frame/mutable.rs
scripts/frame-transpose | rustfmt > target/frame/transpose.rs

cp target/*.rs ../src/model/
cp target/frame/*.rs ../src/model/frame/
33 changes: 28 additions & 5 deletions gen/src/peppi_codegen/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,12 @@
(def tuple-struct?
(complement (comp :name first)))

(defn mutable
[ty]
(str "Mutable" ty))

(defn wrap-transpose
[call]
[:method-call {:unwrap true} call "transpose"])

(defn wrap-map
[target binding-name method-call]
;{:pre [(#{:method-call :fn-call} (first method-call))]}
(let [map-call [:method-call
target
"map"
Expand All @@ -94,6 +89,34 @@
[x]
[:method-call x "unwrap"])

(defn if-ver
([ver then]
(if-ver ver then nil))
([ver then else]
[:if
[:method-call "version" "gte" ver]
(cond->> then
(not= :block (first then)) (conj [:block]))
(cond->> else
(and else (not= :block (first else))) (conj [:block]))]))

(defn nested-version-ifs
[f fields]
(->> fields
(partition-by :version)
reverse
(reduce (fn [acc fields]
(let [ver (:version (first fields))
stmts (concat (mapv f fields) acc)]
(if ver
[(if-ver ver (into [:block] stmts))]
stmts)))
[])))

;;;
;;; AST emitters
;;;

(defmulti emit-expr*
(fn [props & _]
(:type props)))
Expand Down
Loading

0 comments on commit 082a46a

Please sign in to comment.