Skip to content

Commit

Permalink
Merge pull request #60 from niuez/v0.6
Browse files Browse the repository at this point in the history
update to v0.6
  • Loading branch information
niuez authored Sep 20, 2021
2 parents d80e8df + afa91cc commit 74a0d06
Show file tree
Hide file tree
Showing 24 changed files with 298 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "niu"
version = "0.4.2"
version = "0.6.0"
authors = ["niuez <[email protected]>"]
edition = "2018"

Expand Down
6 changes: 6 additions & 0 deletions examples/char.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() -> void {
let a = 'a';
let b = '\"';
let c = '\x7F';
let d = '\'';
}
17 changes: 17 additions & 0 deletions examples/neg.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "std/i64.niu"

struct Hoge {
a: i64,
} {}

impl Neg for Hoge {
type Output = i64;
fn neg(a: Self) -> i64 {
-a.a
}
}

fn main() -> void {
let a = -16i64;
let b: i64 = -Hoge { a: 91i64 };
}
12 changes: 12 additions & 0 deletions examples/not.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "std/bool.niu"

fn main() -> void {
let x = 1i64;
let y = 2i64;
if !(x == y) {
x;
}
else {
y;
}
}
22 changes: 11 additions & 11 deletions lib/data_structure/segment_tree.niu
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ struct SegmentTree<T> where T: Monoid {
} {
fn init(arr: &Vec<T>) -> Self {
let mut n = 1;
for(n = 1; n < arr.len(); n = n * 2) {};
for n = 1; n < arr.len(); n = n * 2 {};
let mut node = Vec::init(2 * n, T#Monoid::ide());
for(let mut i = 0; i < arr.len(); i = i + 1) {
for i = 0; i < arr.len(); i = i + 1 {
node[i + n] = arr[i];
};
for(let mut i = n - 1; i >= 1; i = i - 1) {
}
for i = n - 1; i >= 1; i = i - 1 {
node[i] = node[i * 2].ope(&node[i * 2 + 1]);
};
}
SegmentTree { node: node, n: n, }
}

Expand All @@ -35,26 +35,26 @@ struct SegmentTree<T> where T: Monoid {

fn update(self: &mut Self, p: u64, x: T) -> void {
self.node[p + self.n] = x;
for(let mut i = (p + self.n) / 2; i >= 1; i = i / 2) {
for i = (p + self.n) / 2; i >= 1; i = i / 2 {
self.node[i] = self.node[i * 2].ope(&self.node[i * 2 + 1]);
};
}
}

fn sum(self: &mut Self, l: u64, r: u64) -> T {
let mut lx = T#Monoid::ide();
let mut rx = T#Monoid::ide();
let mut i = l + self.n;
for(let mut j = r + self.n; i < j; j = j >> 1) {
for j = r + self.n; i < j; j = j >> 1 {
if (i & 1) == 1 {
lx = lx.ope(&self.node[i]);
i = i + 1;
} else {};
} else {}
if (j & 1) == 1 {
j = j - 1;
rx = self.node[j].ope(&rx);
} else {};
} else {}
i = i >> 1;
};
}
lx.ope(&rx)
}
}
6 changes: 3 additions & 3 deletions lib/data_structure/union_find.niu
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct UnionFind {
for(let mut i = 0; i < N; i = i + 1) {
par.push(N);
sz.push(1);
};
}
UnionFind { par: par, sz: sz, N: N, }
}
fn root(self: &mut Self, x: u64) -> u64 {
Expand Down Expand Up @@ -41,9 +41,9 @@ struct UnionFind {
else {
self.sz[xr] = ys + xs;
self.par[yr] = xr;
};
}
}
else {
};
}
}
}
20 changes: 10 additions & 10 deletions lib/math/matrix.niu
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Matrix<F> where F: Field
let mut a = Vec::init(h, Vec::init(h, F::zero()));
for(let mut i = 0;i < h;i = i + 1) {
a[i][i] = F::one();
};
}
Matrix {
a: a,
h: h,
Expand All @@ -43,9 +43,9 @@ struct Matrix<F> where F: Field
if exp & 1 == 1 {
ans = ans * now;
}
else {};
else {}
now = now * now;
};
}
ans
}
}
Expand Down Expand Up @@ -73,8 +73,8 @@ impl<F> Add<Matrix<F>> for Matrix<F> where
for(let mut i = 0; i < self.h; i = i + 1) {
for(let mut j = 0; j < self.w; j = j + 1) {
self[i][j] = self[i][j] + right[i][j];
};
};
}
}
self
}
}
Expand All @@ -86,8 +86,8 @@ impl<F> Sub<Matrix<F>> for Matrix<F> where
for(let mut i = 0; i < self.h; i = i + 1) {
for(let mut j = 0; j < self.w; j = j + 1) {
self[i][j] = self[i][j] - right[i][j];
};
};
}
}
self
}
}
Expand All @@ -101,9 +101,9 @@ impl<F> Mul<Matrix<F>> for Matrix<F> where
for(let mut k = 0; k < right.w; k = k + 1) {
for(let mut j = 0; j < self.w; j = j + 1) {
out[i][k] = out[i][k] + self[i][j] * right[j][k];
};
};
};
}
}
}
out
}
}
17 changes: 15 additions & 2 deletions lib/math/modint.niu
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ struct Modint<M> where M: Mod {
if exp & 1 == 1 {
ans = ans * now;
}
else {};
else {}
now = now * now;
};
}
ans
}
fn inv(self: &Self) -> Self {
Expand Down Expand Up @@ -72,3 +72,16 @@ impl<M> Div<Modint<M>> for Modint<M> where M: Mod {
self * right.inv()
}
}

impl<M> Neg for Modint<M> where M: Mod {
type Output = Self;
fn neg(mut self: Self) -> Self {
if self.a == 0 {
self
}
else {
self.a = M::m() - self.a;
self
}
}
}
6 changes: 6 additions & 0 deletions lib/std/bool.niu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "std/opes.niu"

impl Not for bool {
type Output = bool;
fn not(a: Self) -> bool $${!a}$$
}
4 changes: 4 additions & 0 deletions lib/std/i64.niu
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ impl Rem<i64> for i64 {
type Output = i64;
fn rem(a: Self, b: i64) -> i64 $${a % b}$$
}
impl Neg for i64 {
type Output = i64;
fn neg(a: Self) -> i64 $${-a}$$
}
10 changes: 10 additions & 0 deletions lib/std/opes.niu
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
trait Neg {
type Output;
fn neg(a: Self) -> Self#Neg::Output;
}

trait Not {
type Output;
fn not(a: Self) -> Self#Not::Output;
}

trait BitOr<Arg> {
type Output;
fn bit_or(a: Self, b: Arg) -> Self#BitOr<Arg>::Output;
Expand Down
45 changes: 38 additions & 7 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use nom::character::complete::*;
use nom::multi::*;
use nom::sequence::*;
use nom::combinator::*;
use nom::branch::*;

use crate::statement::{ Statement, parse_statement };
use crate::expression::{ Expression, parse_expression };
use crate::expression::*;
use crate::unify::*;
use crate::trans::*;
use crate::mut_checker::*;
use crate::identifier::Tag;

#[derive(Debug)]
pub struct Block {
Expand Down Expand Up @@ -55,17 +57,46 @@ impl MutCheck for Block {
}


enum BlockElement {
End(Option<Expression>),
Statement(Statement),
}

fn parse_block_end(s: &str) -> IResult<&str, BlockElement> {
let (s, (_, expr, _, _)) = tuple((multispace0, opt(parse_expression), multispace0, char('}')))(s)?;
Ok((s, BlockElement::End(expr)))
}

fn parse_block_statement_with_block(s: &str) -> IResult<&str, BlockElement> {
let (s, (_, expr)) = tuple((multispace0, alt((parse_if_expr, parse_for_expr))))(s)?;
Ok((s, BlockElement::Statement(Statement::Expression(expr, Tag::new()))))
}

fn parse_block_statement_without_block(s: &str) -> IResult<&str, BlockElement> {
let (s, (_, stmt, _, _)) = tuple((multispace0, parse_statement, multispace0, char(';')))(s)?;
Ok((s, BlockElement::Statement(stmt)))
}

pub fn parse_block(s: &str) -> IResult<&str, Block> {
let (s, (vec, _, return_exp, _)) = tuple((many0(tuple((multispace0, parse_statement, multispace0, tag(";")))), multispace0, opt(parse_expression), multispace0))(s)?;
let (mut s, _) = tuple((char('{'), multispace0))(s)?;
let mut statements = Vec::new();
for (_, st, _, _) in vec {
statements.push(st);
loop {
match alt((parse_block_end, parse_block_statement_with_block, parse_block_statement_without_block))(s)? {
(s, BlockElement::End(return_exp)) => {
break Ok((s, Block { statements, return_exp }))
}
(ss, BlockElement::Statement(stmt)) => {
s = ss;
statements.push(stmt);
}
}
}
Ok((s, Block { statements, return_exp,}))
}

#[test]
fn parse_block_test() {
println!("{:?}", parse_block("let x = 0; let y = 91; let z = 1333; func(x * x, y, z);").ok());
println!("{:?}", parse_block("let x = 0; let y = 91; let z = 1333; func(x * x, y, z)").ok());
println!("{:?}", parse_block("{ let x = 0; let y = 91; let z = 1333; func(x * x, y, z); }").unwrap());
println!("{:?}", parse_block("{ let x = 0; let y = 91; let z = 1333; func(x * x, y, z) }").unwrap());
println!("{:?}", parse_block("{ let x = 0; let y = 91; if x == y { x; } else { y; } func(x * x, y, z); }").unwrap());
println!("{:?}", parse_block("{ let x = 0; let y = 91; if x == y { x } else { y } }").unwrap());
}
42 changes: 41 additions & 1 deletion src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ pub enum ExpUnaryOpe {
Ref(Box<ExpUnaryOpe>),
MutRef(Box<ExpUnaryOpe>),
Deref(Box<ExpUnaryOpe>, Tag),
Neg(Box<ExpUnaryOpe>, Tag),
Not(Box<ExpUnaryOpe>, Tag),
}

impl GenType for ExpUnaryOpe {
Expand All @@ -855,6 +857,24 @@ impl GenType for ExpUnaryOpe {
equs.add_equation(alpha.clone(), right);
Ok(Type::Deref(Box::new(alpha)))
}
Self::Neg(ref exp, ref tag) => {
Ok(Type::CallEquation( CallEquation {
caller_type: None,
trait_gen: Some(TraitGenerics { trait_id: TraitId { id: Identifier::from_str("Neg") } , generics: Vec::new() }),
func_id: Identifier::from_str("operator-"),
args: vec![exp.gen_type(equs, trs)?],
tag: tag.clone(),
}))
}
Self::Not(ref exp, ref tag) => {
Ok(Type::CallEquation( CallEquation {
caller_type: None,
trait_gen: Some(TraitGenerics { trait_id: TraitId { id: Identifier::from_str("Not") } , generics: Vec::new() }),
func_id: Identifier::from_str("operator!"),
args: vec![exp.gen_type(equs, trs)?],
tag: tag.clone(),
}))
}
}
}
}
Expand All @@ -866,6 +886,8 @@ impl Transpile for ExpUnaryOpe {
Self::Ref(ref exp) => format!("&{}", exp.as_ref().transpile(ta)),
Self::MutRef(ref exp) => format!("&{}", exp.as_ref().transpile(ta)),
Self::Deref(ref exp, _) => format!("*{}", exp.as_ref().transpile(ta)),
Self::Neg(ref exp, _) => format!("-{}", exp.as_ref().transpile(ta)),
Self::Not(ref exp, _) => format!("!{}", exp.as_ref().transpile(ta)),
}
}
}
Expand All @@ -892,6 +914,14 @@ impl MutCheck for ExpUnaryOpe {
_ => Ok(MutResult::NotMut),
}
}
Self::Neg(ref exp, _) => {
exp.mut_check(ta, vars)?;
Ok(MutResult::NotMut)
}
Self::Not(ref exp, _) => {
exp.mut_check(ta, vars)?;
Ok(MutResult::NotMut)
}
}
}
}
Expand All @@ -910,13 +940,23 @@ pub fn parse_exp_unary_ope_deref(s: &str) -> IResult<&str, ExpUnaryOpe> {
Ok((s, ExpUnaryOpe::Deref(Box::new(exp), Tag::new())))
}

pub fn parse_exp_unary_ope_neg(s: &str) -> IResult<&str, ExpUnaryOpe> {
let (s, (_, _, exp)) = tuple((char('-'), multispace0, parse_exp_unary_ope))(s)?;
Ok((s, ExpUnaryOpe::Neg(Box::new(exp), Tag::new())))
}

pub fn parse_exp_unary_ope_not(s: &str) -> IResult<&str, ExpUnaryOpe> {
let (s, (_, _, exp)) = tuple((char('!'), multispace0, parse_exp_unary_ope))(s)?;
Ok((s, ExpUnaryOpe::Not(Box::new(exp), Tag::new())))
}

pub fn parse_exp_unary_ope_unary_exp(s: &str) -> IResult<&str, ExpUnaryOpe> {
let (s, exp) = parse_unary_expr(s)?;
Ok((s, ExpUnaryOpe::UnaryExpr(exp)))
}

pub fn parse_exp_unary_ope(s: &str) -> IResult<&str, ExpUnaryOpe> {
alt((parse_exp_unary_ope_mutref, parse_exp_unary_ope_ref, parse_exp_unary_ope_deref, parse_exp_unary_ope_unary_exp))(s)
alt((parse_exp_unary_ope_mutref, parse_exp_unary_ope_ref, parse_exp_unary_ope_deref, parse_exp_unary_ope_neg, parse_exp_unary_ope_not, parse_exp_unary_ope_unary_exp))(s)
}


Expand Down
Loading

0 comments on commit 74a0d06

Please sign in to comment.