提交 1eab7f15 编写于 作者: V visus 提交者: 衍星居士

【凹中文】基本完成了烧脑虚拟机的示例

上级 b5298241
// 版权 @2022 _examples/hello 作者。保留所有权利。
引于 "书"
引于 "烧脑虚拟机/烧脑"
【启】:
// 基本函数调用
书·说:"你好,凹语言!"
设代码="++++++++++[>++++++++++<-]>++++.+."
设虚拟机=烧脑·新机:代码
虚拟机·启动()
\ No newline at end of file
引于 "书"
《烧脑》:
内存之[30000]byte
序之数 // 虚拟机程序计数器的序数
位之数 // 当前内存指针的位置
代码之文 // 需要运行的代码
【新机】(代码之文) => *烧脑:
归于:&烧脑{代码: 代码}
【烧脑·转】(步之数):
从i=步,到i==0,有:
设字=此·代码[此·序+步]
当字:
为'[',则:i++
为']',则:i--
此·序+=步
【烧脑·启动】:
设结束=len:此·代码
直到此·序==结束,有:
设甲=此·代码[此·序]
当甲:
为'>',则:此·位++
为'<',则:此·位--
为'+',则:此·内存[此·位]++
为'-',则:此·内存[此·位]--
为'[',则:
若此·内存[此·位]==0则:
此·转:1
为']',则:
若此·内存[此·位]!=0则:
此·转:-1
为'.',则:书·字:此·内存[此·位]
此·序++
......@@ -21,8 +21,11 @@
// 简单的自定义类型
设某=点{横:10, 纵:2}
书·说:"[自定义类型]点的纵坐标和横坐标之和:"
书·说:"[自定义类型]点(10,2)的纵坐标和横坐标之和:"
书·曰:某·横 + 某·纵
书·说:"[自定义类型]点(10,2)的纵坐标和横坐标之平方和:"
书·曰:某·平方距:
// 循环
......@@ -38,6 +41,13 @@
书·曰:j
书·说:"[循环语句] 从10到0:"
设步=1
从i=10,到i>=100,有:
i+=步
书·曰:i
// 类似until语句
书·说:"[直到语句] 直到5:"
设i=0
......@@ -62,6 +72,10 @@
纵之数
【点·平方距】() => 数 :
归于:此·纵*此·纵 + 此·横*此·横
【加】(甲, 乙之数) => 数 :
归于:甲+乙
\ No newline at end of file
......@@ -1963,7 +1963,7 @@ func (p *parser) 解析条件语句() *ast.IfStmt {
else_ = &ast.BadStmt{From: p.pos, To: p.pos}
}
} else {
p.expectSemi()
// p.expectSemi()
}
return &ast.IfStmt{If: pos, Init: init, Cond: cond, Body: body, Else: else_}
......@@ -2668,7 +2668,25 @@ func (p *parser) 解析函数定义() *ast.FuncDecl {
recv = p.parseParameters(scope, false)
}
// 函数名
ident := p.parseIdent()
if recv == nil && p.tok == token.PERIOD {
thisIdent := &ast.Ident{Name: "此"}
thisField := &ast.Field{
Names: []*ast.Ident{thisIdent},
Type: &ast.StarExpr{X: ident},
}
recv = &ast.FieldList{
List: []*ast.Field{thisField},
}
p.declare(thisField, nil, scope, ast.Var, thisIdent)
p.next()
ident = p.parseIdent()
}
pos := p.expect(token.TK_右方)
params, results, arrowPos := p.parseSignature(scope)
......@@ -2952,18 +2970,16 @@ func (p *parser) 解析三段循环语句() ast.Stmt {
defer p.closeScope()
init, _ := p.parseSimpleStmt(initDefine)
/*
if as, ok := init.(*ast.AssignStmt); ok {
as.Tok = token.ToWaTok(token.DEFINE)
p.shortVarDecl(as, as.Lhs)
}
*/
p.expect(token.TK_逗号)
p.expect(token.TK_到)
cond, _ := p.parseSimpleStmt(basic)
p.expect(token.TK_逗号)
p.expect(token.TK_有)
post, _ := p.parseSimpleStmt(basic)
var post ast.Stmt
if p.tok != token.TK_冒号 {
post, _ = p.parseSimpleStmt(basic)
}
body := p.解析语句块()
......@@ -3032,7 +3048,6 @@ func (p *parser) 解析择路语句() ast.Stmt {
list = append(list, p.解析择路分支())
}
rbrace := p.expect(token.TK_句号)
p.expectSemi()
body := &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
return &ast.SwitchStmt{Switch: pos, Init: nil, Tag: id, Body: body}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册