From 9dfe54a573614bdfd68004a23f6e9fd9c53d951e Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 25 Dec 2020 22:01:59 +0000 Subject: [PATCH] [Bootstrap]: Add internal instructions with an IExpr interface --- bootstrap/pkg/ast/ast.go | 1 + bootstrap/pkg/core/instructions.go | 71 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 bootstrap/pkg/core/instructions.go diff --git a/bootstrap/pkg/ast/ast.go b/bootstrap/pkg/ast/ast.go index 70d7a8a..298ce47 100644 --- a/bootstrap/pkg/ast/ast.go +++ b/bootstrap/pkg/ast/ast.go @@ -27,6 +27,7 @@ const ( Nothing True False + Instruction Symbol Keyword Number diff --git a/bootstrap/pkg/core/instructions.go b/bootstrap/pkg/core/instructions.go new file mode 100644 index 0000000..a88058b --- /dev/null +++ b/bootstrap/pkg/core/instructions.go @@ -0,0 +1,71 @@ +/* + Serene --- Yet an other Lisp + +Copyright (c) 2020 Sameer Rahmani + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package core + +import ( + "fmt" + + "serene-lang.org/bootstrap/pkg/ast" + "serene-lang.org/bootstrap/pkg/hash" +) + +type instructionType int + +const ( + PopStack instructionType = iota +) + +type Instruction struct { + Node + ExecutionScope + Type instructionType +} + +func (n *Instruction) GetType() ast.NodeType { + return ast.Instruction +} + +func (n *Instruction) String() string { + return fmt.Sprintf("", n.Type) +} + +func (n *Instruction) ToDebugStr() string { + return n.String() +} + +func (n *Instruction) Hash() uint32 { + bytes := []byte(fmt.Sprintf("%d", n.Type)) + return hash.HashOf(append([]byte{byte(ast.Instruction)}, bytes...)) +} + +func MakeStackPop(rt *Runtime) IExpr { + return &Instruction{ + Type: PopStack, + } +} + +func ProcessInstruction(rt *Runtime, form *Instruction) IError { + switch form.Type { + case PopStack: + rt.Stack.Pop() + return nil + default: + panic(fmt.Sprintf("Unknown instruction: '%d'", form.Type)) + } +}