类型验证系统的设计

类型验证系统能发现许多代码中的错误,能够帮助程序员快速找到这些错误,尽快构建正确的代码。程序员

fn func-name(arg1: Str arg2: Bool) -> Bool
  return true

类型验证系统须要首先收集函数的定义:正则表达式

func-name:Str:Bool -> Bool

而后会对代码出现的函数调用进行类型验证:函数

if func-name("filter", true) return true

若是要支持不定参数或可选参数,那么类型系统的验证就须要进行相似正则的运算:this

fn func-name(args: Str+) return args.join('|')
fn func-name(arg: Bool?) if is-define(args) return true

类型系统获得了参数的类型后,用预约义的函数类型 Pattern 去 Match 获得的 Type Str..net

用正则表达式来实现的话,移植性会出现问题,由于这须要语言支持对正则表达式的正则插值。如今我只发现 Perl 支持,别的语言对于正则的功能就有限多了。因此一涉及到正则,语言的可移植性就出现了问题。rest

用现有的匹配系统完成这个工做,虽然效率低些,但若是写成低级语言,仍是一样高效。code

首先定义一个 type grammar 来定义参数的 pattern 规则,十分简单:token

Str :Str Str+ Str?

只是支持字符串,符号,重复匹配和无关紧要的语法:字符串

type = |Str Token Rept|+ $
        Str   = ':' [\a\-] ;
        Token = [\a\-]+ ;

用一个 Int+ 的字符串去匹配 Int Int 的字符串,很简单的啊。get

泛型函数,支持多种参数的同名函数,类型有分支,还有不一样的返回值。

这用正则,就很是很是复杂了,仍是老实用本身的系统把。

(return return-type) 将返回类型放在最后的表达式中。

也就是说返回值是在匹配之后才会得到,若是不匹配,就返回错误,若是正确 就返回类型,这要频繁切换匹配文本,还有类型别名,要创建一个现成的 curosr, 重置 cursor 到 off -> 0, 切换 text 到新的文本,只有分支和字符串没有别的 用到表达式,添加 return 字符串,cursor 用 reset text 预备好 rule estr

match rules opt-rules, grammar no door rule, 
only have some token; token only have define
match-rule: pattern to match pattern
opt-match to get rule ast
use this rule to match-rule , text use reset (cursor, $text)
no match no return [@type](https://my.oschina.net/fanren1919) , return would check it if is same with define
if no problem, then call would return its define
many call have same name, then combin together with pattern 
then could check rest string -> string rest array -> return array
only could make code is more readable and simple, and is writeable 
with many language.

Pattern define:

Pattern = Int Int -> Int | Str Str -> Str

Branch use |...| is hard to combin, use a | b | c is simple to combin

So Type Grammar is not same with Spp, and have own Optimizer code.

Branch is define use | opt, define

相关文章
相关标签/搜索