Skip to content

Commit ee4f331

Browse files
committed
Add import-as syntax, VM performance optimizations, fix HTTP callbacks, bump to v1.3.0
- Add 'import <module> as <alias>' syntax with OP_IMPORT_AS opcode - VM execute loop: cache frame fields as locals, inline readByte/readUint16 - Add small integer cache (-128..127) and singleton true/false/nil values - Inline fast paths for int+int add/sub/compare in the hot loop - Fix HTTP handler callback: use stopFP parameter in execute() so callbacks return to the caller instead of falling through to parent frames - Document import aliases in imports.md
1 parent 900ff68 commit ee4f331

7 files changed

Lines changed: 314 additions & 98 deletions

File tree

ccolon/compiler/compiler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,16 @@ func (c *Compiler) compileImport(s *parser.ImportStmt) error {
182182
idx := c.addConstant(s.Module)
183183
if s.IsFile {
184184
c.emitOp(OP_IMPORT_FILE, s.P.Line)
185+
c.emitUint16(idx, s.P.Line)
186+
} else if s.Alias != "" {
187+
aliasIdx := c.addConstant(s.Alias)
188+
c.emitOp(OP_IMPORT_AS, s.P.Line)
189+
c.emitUint16(idx, s.P.Line)
190+
c.emitUint16(aliasIdx, s.P.Line)
185191
} else {
186192
c.emitOp(OP_IMPORT, s.P.Line)
193+
c.emitUint16(idx, s.P.Line)
187194
}
188-
c.emitUint16(idx, s.P.Line)
189195
return nil
190196
}
191197

ccolon/compiler/opcode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const (
6767
OP_MARK_CONST
6868
OP_CALL_KW
6969
OP_FROM_IMPORT
70+
OP_IMPORT_AS
7071
)
7172

7273
type FuncObject struct {
@@ -122,6 +123,7 @@ var opNames = map[OpCode]string{
122123
OP_MARK_CONST: "MARK_CONST",
123124
OP_CALL_KW: "CALL_KW",
124125
OP_FROM_IMPORT: "FROM_IMPORT",
126+
OP_IMPORT_AS: "IMPORT_AS",
125127
}
126128

127129
func (op OpCode) String() string {

ccolon/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/TRC-Loop/ccolon/vm"
1818
)
1919

20-
const version = "1.2.2"
20+
const version = "1.3.0"
2121

2222
func main() {
2323
if len(os.Args) < 2 {

ccolon/parser/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ type FuncDecl struct {
265265

266266
type ImportStmt struct {
267267
Module string
268+
Alias string // empty means no alias
268269
IsFile bool
269270
P Position
270271
}

ccolon/parser/parser.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,19 @@ func (p *Parser) parseImport() (*ImportStmt, error) {
153153
if err != nil {
154154
return nil, err
155155
}
156-
return &ImportStmt{Module: name.Literal, P: Position{tok.Line, tok.Col}}, nil
156+
157+
// import <module> as <alias>
158+
var alias string
159+
if p.current().Type == lexer.TOKEN_AS {
160+
p.advance() // consume 'as'
161+
aliasTok, err := p.expect(lexer.TOKEN_IDENT)
162+
if err != nil {
163+
return nil, err
164+
}
165+
alias = aliasTok.Literal
166+
}
167+
168+
return &ImportStmt{Module: name.Literal, Alias: alias, P: Position{tok.Line, tok.Col}}, nil
157169
}
158170

159171
func (p *Parser) parseFromImport() (*FromImportStmt, error) {

0 commit comments

Comments
 (0)