Skip to content

Commit 01be3a3

Browse files
authored
Merge #48: Support using sub-modules/components directly
See #27 and the ChangeLog
2 parents 9300067 + 16fb8ca commit 01be3a3

File tree

11 files changed

+132
-101
lines changed

11 files changed

+132
-101
lines changed

ChangeLog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ChangeLog for Anole
22

3-
## Unreleased
3+
## 0.0.22 - 2021/01/24
44

55
### Added
66

@@ -10,17 +10,23 @@
1010

1111
- Only Anole modules can be imported by simple name now
1212
- Cpp modules must be imported by using their direct paths
13+
- Instruction `ImportAll` is unary operation now
1314

1415
### Updated
1516

1617
- Improve the performance of REPL
1718
- Improve the error information of non-defined variables which may cause segmentation fault
1819
- Make sure all paths could be finded correctly
20+
- Support using sub-modules/components directly now, see [ENHANCEMENT#27](https://github.com/anole-lang/anole/issues/27)
1921

2022
### Fixed
2123

2224
- Fix the bug caused by no-block-scope when using nested foreach-stmt, see more in [BUG#40](https://github.com/anole-lang/anole/issues/40)
2325

26+
### Removed
27+
28+
- Remove instruction `ImportAllPath`
29+
2430
## 0.0.21 - 2020/11/24
2531

2632
### Added

anole/compiler/ast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ QuesExpr::QuesExpr(Ptr<Expr> &&cond, Ptr<Expr> &&true_expr, Ptr<Expr> &&false_ex
9393
// ...
9494
}
9595

96-
UseStmt::UseStmt(Aliases &&aliases, Module from) noexcept
96+
UseStmt::UseStmt(Aliases &&aliases, UseStmt::NestedModule &&from) noexcept
9797
: aliases(std::move(aliases)), from(std::move(from))
9898
{
9999
// ...

anole/compiler/ast.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,24 @@ struct UseStmt : Stmt
247247
enum class Type
248248
{
249249
Name,
250-
Path,
251-
Null
250+
Path
252251
};
252+
253253
String mod;
254254
Type type;
255255
};
256+
using NestedModule = std::vector<Module>;
256257

257-
// second String is the alias
258-
using Alias = std::pair<Module, String>;
258+
// second String for the alias
259+
using Alias = std::pair<NestedModule, String>;
259260
using Aliases = std::list<Alias>;
260261

261-
// aliases are empty means `use *`
262+
// empty alias means `use *`
262263
Aliases aliases;
263-
// from may be a name or a path
264-
Module from;
264+
// may be empty
265+
NestedModule from;
265266

266-
UseStmt(Aliases &&, Module) noexcept;
267+
UseStmt(Aliases &&, NestedModule &&) noexcept;
267268
void codegen(Code &) override;
268269
};
269270

anole/compiler/code.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ void Code::print(std::ostream &out)
342342
printer.add_line(i, "ImportPath", OPRAND(String));
343343
break;
344344
case Opcode::ImportAll:
345-
printer.add_line(i, "ImportAll", OPRAND(String));
346-
break;
347-
case Opcode::ImportAllPath:
348-
printer.add_line(i, "ImportAllPath", OPRAND(String));
345+
printer.add_line(i, "ImportAll");
349346
break;
350347
case Opcode::ImportPart:
351348
printer.add_line(i, "ImportPart", OPRAND(String));
@@ -570,8 +567,6 @@ void Code::serialize(std::ostream &out)
570567

571568
case Opcode::Import:
572569
case Opcode::ImportPath:
573-
case Opcode::ImportAll:
574-
case Opcode::ImportAllPath:
575570
case Opcode::ImportPart:
576571
case Opcode::Load:
577572
case Opcode::LoadMember:
@@ -690,8 +685,6 @@ bool Code::unserialize(std::ifstream &in)
690685

691686
case Opcode::Import:
692687
case Opcode::ImportPath:
693-
case Opcode::ImportAll:
694-
case Opcode::ImportAllPath:
695688
case Opcode::ImportPart:
696689
case Opcode::Load:
697690
case Opcode::LoadMember:

anole/compiler/codegen.cpp

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -477,52 +477,70 @@ void QuesExpr::codegen(Code &code)
477477

478478
void UseStmt::codegen(Code &code)
479479
{
480-
if (from.type == Module::Type::Null)
480+
auto part_import = [&code] (auto begin, auto end) mutable
481+
{
482+
for (auto it = begin; it != end; ++it)
483+
{
484+
// assume type is name
485+
code.add_ins<Opcode::ImportPart, String>(it->mod);
486+
}
487+
};
488+
489+
auto module_import = [&code, &part_import] (auto begin, auto end) mutable
490+
{
491+
if (begin->type == Module::Type::Name)
492+
{
493+
code.add_ins<Opcode::Import, String>(begin->mod);
494+
}
495+
else
496+
{
497+
code.add_ins<Opcode::ImportPath, String>(begin->mod);
498+
}
499+
part_import(begin + 1, end);
500+
};
501+
502+
if (from.empty())
481503
{
482504
for (auto &alias : aliases)
483505
{
484-
if (alias.first.type == Module::Type::Name)
485-
{
486-
code.add_ins<Opcode::Import, String>(alias.first.mod);
487-
}
488-
else
506+
module_import(alias.first.begin(), alias.first.end());
507+
code.add_ins<Opcode::StoreRef, String>(alias.second);
508+
509+
if (alias.first.size() > 1)
489510
{
490-
code.add_ins<Opcode::ImportPath, String>(alias.first.mod);
511+
code.add_ins<Opcode::FastPop, Size>(alias.first.size() - 1);
491512
}
492-
code.add_ins<Opcode::StoreRef, String>(alias.second);
493513
}
494514
}
495515
else
496516
{
497517
// means `use *`
498518
if (aliases.empty())
499519
{
500-
if (from.type == Module::Type::Name)
501-
{
502-
code.add_ins<Opcode::ImportAll, String>(from.mod);
503-
}
504-
else
520+
module_import(from.begin(), from.end());
521+
code.add_ins<Opcode::ImportAll>();
522+
523+
if (from.size() > 1)
505524
{
506-
code.add_ins<Opcode::ImportAllPath, String>(from.mod);
525+
code.add_ins<Opcode::FastPop, Size>(from.size() - 1);
507526
}
508527
}
509528
else
510529
{
511-
if (from.type == Module::Type::Name)
512-
{
513-
code.add_ins<Opcode::Import, String>(from.mod);
514-
}
515-
else
516-
{
517-
code.add_ins<Opcode::ImportPath, String>(from.mod);
518-
}
530+
module_import(from.begin(), from.end());
519531

520532
for (auto &alias : aliases)
521533
{
522-
code.add_ins<Opcode::ImportPart, String>(alias.first.mod);
534+
part_import(alias.first.begin(), alias.first.end());
523535
code.add_ins<Opcode::StoreRef, String>(alias.second);
536+
537+
if (alias.first.size() > 1)
538+
{
539+
code.add_ins<Opcode::FastPop, Size>(alias.first.size() - 1);
540+
}
524541
}
525-
code.add_ins<Opcode::FastPop>();
542+
543+
code.add_ins<Opcode::FastPop, Size>(from.size());
526544
}
527545
}
528546
}
@@ -538,7 +556,7 @@ void ExprStmt::codegen(Code &code)
538556
else
539557
{
540558
expr->codegen(code);
541-
code.add_ins<Opcode::FastPop>();
559+
code.add_ins<Opcode::FastPop, Size>(1);
542560
}
543561
}
544562

anole/compiler/instruction.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ enum class Opcode : uint8_t
1010
PlaceHolder,
1111

1212
Pop, // Pop
13-
FastPop, // FastPop
13+
FastPop, // FastPop num
1414

1515
Import, // Import name
1616
ImportPath, // ImportPath path
17-
ImportAll, // ImportAll name
18-
ImportAllPath,// ImportAllPath path
17+
ImportAll, // ImportAll
1918
ImportPart, // ImportPart name
2019

2120
Load, // Load name

0 commit comments

Comments
 (0)