Forth implemented in Rust trait system

栏目: IT技术 · 发布时间: 5年前

内容简介:Compile-time compiler that compiles Forth to compile-time trait expressions.Rust's trait system is Turing complete. This crate uses the principles fromHere's a simple factorial implementation, the only non-standard word here is

fortraith

Compile-time compiler that compiles Forth to compile-time trait expressions.

What?

Rust's trait system is Turing complete. This crate uses the principles from trait-eval to implement necessary traits for forth evalutaion and provides a forth! macro that transpiles forth's syntax to trait expressions.

Show me!

Here's a simple factorial implementation, the only non-standard word here is pred which is a decrement operator, equivalent to 1 - :

forth!(
    : factorial (n -- n) 1 swap fact0 ;
    : fact0 (n n -- n) dup 1 = if drop else dup rot * swap pred fact0 then ;
    5 factorial .
);

This prints 120 . As you can see not only you can define functions easily, but even conditional recursion is possible! Now check out how it looks compiled to trait expressions (courtesy of cargo expand ):

pub trait factorial {
   type Result;
}
impl<V, N> factorial for Node<V, N>
where
    Self: one,
    <Self as one>::Result: swap,
    <<Self as one>::Result as swap>::Result: fact0,
{
    type Result = <<<Self as one>::Result as swap>::Result as fact0>::Result;
}
pub trait fact0 {
    type Result;
}
impl <V ,N> fact0 for Node <V ,N>
where
    Self: dup,
    <Self as dup>::Result: one,
    <<Self as dup>::Result as one>::Result: eq,
    <<<Self as dup>::Result as one>::Result as eq>::Result: iff,
    <<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result: drop,
    <<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result: elsef,
    <<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result: dup,
    <<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result: rot,
    <<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result: mult,
    <<<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result as mult>::Result: swap,
    <<<<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result as mult>::Result as swap>::Result: pred,
    <<<<<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result as mult>::Result as swap>::Result as pred>::Result: fact0,
    <<<<<<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result as mult>::Result as swap>::Result as pred>::Result as fact0>::Result: then
{
    type Result = <<<<<<<<<<<<<Self as dup>::Result as one>::Result as eq>::Result as iff>::Result as drop>::Result as elsef>::Result as dup>::Result as rot>::Result as mult>::Result as swap>::Result as pred>::Result as fact0>::Result as then>::Result;
}
println!("{}", <<<Empty as five>::Result as factorial>::Result as top>::Result::eval());

Yeah, writing that manually would be no fun.

What can I do with it?

Quite a bit is actually supported as you can see above. Every operation from trait-eval is re-exported to work on the stack (except if which is done differently), and a faw additional stack operations are provided. See docs for the details.


以上所述就是小编给大家介绍的《Forth implemented in Rust trait system》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法导论(原书第3版)

算法导论(原书第3版)

Thomas H.Cormen、Charles E.Leiserson、Ronald L.Rivest、Clifford Stein / 殷建平、徐云、王刚、刘晓光、苏明、邹恒明、王宏志 / 机械工业出版社 / 2012-12 / 128.00元

在有关算法的书中,有一些叙述非常严谨,但不够全面;另一些涉及了大量的题材,但又缺乏严谨性。本书将严谨性和全面性融为一体,深入讨论各类算法,并着力使这些算法的设计和分析能为各个层次的读者接受。全书各章自成体系,可以作为独立的学习单元;算法以英语和伪代码的形式描述,具备初步程序设计经验的人就能看懂;说明和解释力求浅显易懂,不失深度和数学严谨性。 全书选材经典、内容丰富、结构合理、逻辑清晰,对本科......一起来看看 《算法导论(原书第3版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试