parsing, lexing, some compiling of const
All checks were successful
/ test (push) Successful in 1m1s
All checks were successful
/ test (push) Successful in 1m1s
This commit is contained in:
parent
beaa325c7b
commit
f54d8304fc
5 changed files with 91 additions and 10 deletions
|
|
@ -33,6 +33,7 @@ type ImportsResolver interface {
|
||||||
type LocalVariable struct {
|
type LocalVariable struct {
|
||||||
name string
|
name string
|
||||||
signature TypeSignature
|
signature TypeSignature
|
||||||
|
value Value
|
||||||
scope int
|
scope int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -415,6 +416,25 @@ func (c *Compiler) compile(tree Node) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case ConstNodeType:
|
||||||
|
n := tree.(*ConstNode)
|
||||||
|
|
||||||
|
if !c.isTreeConstant(n.value) {
|
||||||
|
return c.error(fmt.Sprintf("value of %s is not constant", n.name), n.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := c.compute(n.value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := c.deduceSignature(n)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
c.registerConst(n.name, t, value)
|
||||||
|
|
||||||
case CallNodeType:
|
case CallNodeType:
|
||||||
n := tree.(*CallNode)
|
n := tree.(*CallNode)
|
||||||
|
|
||||||
|
|
@ -1023,6 +1043,16 @@ func (c *Compiler) registerVar(name string, t TypeSignature) {
|
||||||
c.stack.Push(LocalVariable{
|
c.stack.Push(LocalVariable{
|
||||||
name,
|
name,
|
||||||
t,
|
t,
|
||||||
|
nil,
|
||||||
|
int(c.scope),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Compiler) registerConst(name string, t TypeSignature, constant Value) {
|
||||||
|
c.stack.Push(LocalVariable{
|
||||||
|
name,
|
||||||
|
t,
|
||||||
|
constant,
|
||||||
int(c.scope),
|
int(c.scope),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ const (
|
||||||
TokenVar
|
TokenVar
|
||||||
TokenIf
|
TokenIf
|
||||||
TokenElse
|
TokenElse
|
||||||
|
TokenConst
|
||||||
TokenImport
|
TokenImport
|
||||||
|
|
||||||
TokenComma
|
TokenComma
|
||||||
|
|
@ -349,6 +350,8 @@ func (l *Lexer) NextToken() (Token, error) {
|
||||||
return l.makeToken(TokenReturn), nil
|
return l.makeToken(TokenReturn), nil
|
||||||
case "import":
|
case "import":
|
||||||
return l.makeToken(TokenImport), nil
|
return l.makeToken(TokenImport), nil
|
||||||
|
case "const":
|
||||||
|
return l.makeToken(TokenConst), nil
|
||||||
default:
|
default:
|
||||||
return l.makeToken(TokenName), nil
|
return l.makeToken(TokenName), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ const (
|
||||||
ConditionalNodeType
|
ConditionalNodeType
|
||||||
LoopNodeType
|
LoopNodeType
|
||||||
AssignNodeType
|
AssignNodeType
|
||||||
|
ConstNodeType
|
||||||
CallNodeType
|
CallNodeType
|
||||||
FunctionNodeType
|
FunctionNodeType
|
||||||
ReturnNodeType
|
ReturnNodeType
|
||||||
|
|
@ -75,6 +76,8 @@ func (n NodeType) String() string {
|
||||||
return "Breakpoint"
|
return "Breakpoint"
|
||||||
case UnaryNodeType:
|
case UnaryNodeType:
|
||||||
return "Unary"
|
return "Unary"
|
||||||
|
case ConstNodeType:
|
||||||
|
return "Const"
|
||||||
}
|
}
|
||||||
return "Invalid Node Type"
|
return "Invalid Node Type"
|
||||||
}
|
}
|
||||||
|
|
@ -476,6 +479,26 @@ func (n AssignNode) Bounds() (Pos, Pos) {
|
||||||
return n.start, n.end
|
return n.start, n.end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConstNode struct {
|
||||||
|
name string
|
||||||
|
value Node
|
||||||
|
|
||||||
|
start Pos
|
||||||
|
end Pos
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n ConstNode) Type() NodeType {
|
||||||
|
return ConstNodeType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n ConstNode) String() string {
|
||||||
|
return fmt.Sprintf("define const %s to be %s", n.name, n.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n ConstNode) Bounds() (Pos, Pos) {
|
||||||
|
return n.start, n.end
|
||||||
|
}
|
||||||
|
|
||||||
// CallNode function call
|
// CallNode function call
|
||||||
type CallNode struct {
|
type CallNode struct {
|
||||||
source Node
|
source Node
|
||||||
|
|
|
||||||
|
|
@ -816,6 +816,31 @@ func (p *Parser) statement() (Node, error) {
|
||||||
defer p.advance()
|
defer p.advance()
|
||||||
return nil, p.error("import statements must be top-level", p.curr)
|
return nil, p.error("import statements must be top-level", p.curr)
|
||||||
|
|
||||||
|
case TokenConst:
|
||||||
|
p.advance()
|
||||||
|
start := p.prev.Start
|
||||||
|
|
||||||
|
if err := p.expect(TokenName, "a const must be referred to by a name"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
name := p.prev.Lexeme
|
||||||
|
|
||||||
|
if err := p.expect(TokenAssign, "const values must have an assigned value"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := p.condition()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ConstNode{
|
||||||
|
name,
|
||||||
|
value,
|
||||||
|
start,
|
||||||
|
p.prev.Start + p.prev.Length,
|
||||||
|
}, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
defer p.advance()
|
defer p.advance()
|
||||||
return nil, p.error("invalid statement", p.curr)
|
return nil, p.error("invalid statement", p.curr)
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@
|
||||||
_bell := char(0x07)
|
_bell := char(0x07)
|
||||||
|
|
||||||
ESC := char(0x1B)
|
ESC := char(0x1B)
|
||||||
CSI := ESC + "["
|
const CSI = ESC + "["
|
||||||
|
|
||||||
_reset := CSI + "0m"
|
const _reset = CSI + "0m"
|
||||||
_bold := CSI + "1m"
|
const _bold = CSI + "1m"
|
||||||
_faint := CSI + "2m"
|
const _faint = CSI + "2m"
|
||||||
_italic := CSI + "3m"
|
const _italic = CSI + "3m"
|
||||||
_underline := CSI + "4m"
|
const _underline = CSI + "4m"
|
||||||
_slow_blink := CSI + "5m"
|
const _slow_blink = CSI + "5m"
|
||||||
_rapid_blink := CSI + "6m"
|
const _rapid_blink = CSI + "6m"
|
||||||
_strike := CSI + "9m"
|
const _strike = CSI + "9m"
|
||||||
_primary_font := CSI + "10m"
|
const _primary_font = CSI + "10m"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue