Skip to content

Commit

Permalink
Public keys, pubkey operations, p2 replica
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 16, 2024
1 parent 98d1b25 commit 796bb21
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 101 deletions.
19 changes: 19 additions & 0 deletions crates/rue-compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct Ops {
div: NodePtr,
divmod: NodePtr,
gt: NodePtr,
point_add: NodePtr,
pubkey_for_exp: NodePtr,
not: NodePtr,
any: NodePtr,
}
Expand All @@ -55,6 +57,8 @@ impl<'a> Codegen<'a> {
div: allocator.new_small_number(19).unwrap(),
divmod: allocator.new_small_number(20).unwrap(),
gt: allocator.new_small_number(21).unwrap(),
point_add: allocator.new_small_number(29).unwrap(),
pubkey_for_exp: allocator.new_small_number(30).unwrap(),
not: allocator.new_small_number(32).unwrap(),
any: allocator.new_small_number(33).unwrap(),
};
Expand All @@ -76,7 +80,9 @@ impl<'a> Codegen<'a> {
Lir::Sha256(value) => self.gen_sha256(value),
Lir::IsCons(value) => self.gen_is_cons(value),
Lir::Strlen(value) => self.gen_strlen(value),
Lir::PubkeyForExp(value) => self.gen_pubkey_for_exp(value),
Lir::Concat(values) => self.gen_concat(values),
Lir::PointAdd(values) => self.gen_point_add(values),
Lir::If(condition, then_branch, else_branch) => {
self.gen_if(condition, then_branch, else_branch)
}
Expand Down Expand Up @@ -161,6 +167,11 @@ impl<'a> Codegen<'a> {
self.list(&[self.ops.strlen, value])
}

fn gen_pubkey_for_exp(&mut self, value: LirId) -> NodePtr {
let value = self.gen_lir(value);
self.list(&[self.ops.pubkey_for_exp, value])
}

fn gen_concat(&mut self, values: Vec<LirId>) -> NodePtr {
let mut args = vec![self.ops.concat];
for value in values {
Expand All @@ -169,6 +180,14 @@ impl<'a> Codegen<'a> {
self.list(&args)
}

fn gen_point_add(&mut self, values: Vec<LirId>) -> NodePtr {
let mut args = vec![self.ops.point_add];
for value in values {
args.push(self.gen_lir(value));
}
self.list(&args)
}

fn gen_if(&mut self, condition: LirId, then_branch: LirId, else_branch: LirId) -> NodePtr {
let condition = self.gen_lir(condition);
let then_branch = self.gen_lir(then_branch);
Expand Down
3 changes: 3 additions & 0 deletions crates/rue-compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pub enum DiagnosticInfo {

#[error("blocks must either have a an expression value, return statement, or raise an error")]
EmptyBlock,

#[error("cannot check equality on non-atom type `{0}`")]
NonAtomEquality(String),
}

/// Join a list of names into a string, wrapped in backticks.
Expand Down
26 changes: 4 additions & 22 deletions crates/rue-compiler/src/hir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rue_parser::BinaryOp;

use crate::database::{HirId, ScopeId, SymbolId};

#[derive(Debug, Clone)]
Expand All @@ -17,7 +15,7 @@ pub enum Hir {
args: HirId,
},
BinaryOp {
op: HirBinaryOp,
op: BinOp,
lhs: HirId,
rhs: HirId,
},
Expand All @@ -28,6 +26,7 @@ pub enum Hir {
Sha256(HirId),
IsCons(HirId),
Strlen(HirId),
PubkeyForExp(HirId),
If {
condition: HirId,
then_block: HirId,
Expand All @@ -36,7 +35,7 @@ pub enum Hir {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HirBinaryOp {
pub enum BinOp {
Add,
Subtract,
Multiply,
Expand All @@ -49,22 +48,5 @@ pub enum HirBinaryOp {
Equals,
NotEquals,
Concat,
}

impl From<BinaryOp> for HirBinaryOp {
fn from(op: BinaryOp) -> Self {
match op {
BinaryOp::Add => HirBinaryOp::Add,
BinaryOp::Subtract => HirBinaryOp::Subtract,
BinaryOp::Multiply => HirBinaryOp::Multiply,
BinaryOp::Divide => HirBinaryOp::Divide,
BinaryOp::Remainder => HirBinaryOp::Remainder,
BinaryOp::LessThan => HirBinaryOp::LessThan,
BinaryOp::GreaterThan => HirBinaryOp::GreaterThan,
BinaryOp::LessThanEquals => HirBinaryOp::LessThanEquals,
BinaryOp::GreaterThanEquals => HirBinaryOp::GreaterThanEquals,
BinaryOp::Equals => HirBinaryOp::Equals,
BinaryOp::NotEquals => HirBinaryOp::NotEquals,
}
}
PointAdd,
}
2 changes: 2 additions & 0 deletions crates/rue-compiler/src/lir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub enum Lir {
Sha256(LirId),
IsCons(LirId),
Strlen(LirId),
PubkeyForExp(LirId),
If(LirId, LirId, LirId),
Not(LirId),
Any(Vec<LirId>),
Concat(Vec<LirId>),
PointAdd(Vec<LirId>),
Add(Vec<LirId>),
Sub(Vec<LirId>),
Mul(Vec<LirId>),
Expand Down
Loading

0 comments on commit 796bb21

Please sign in to comment.