Skip to content

Commit

Permalink
update export and import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kulics committed Apr 30, 2020
1 parent f794234 commit e89545e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
11 changes: 7 additions & 4 deletions Compiler/KParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ statement: (New_Line)* (annotationSupport)?
exportStatement (New_Line)* namespaceSupportStatement*;

// 导出命名空间
exportStatement: nameSpaceItem call left_brace (importStatement|typeAliasStatement|New_Line)* right_brace end;

// 导入命名空间
importStatement: (annotationSupport)? ((id Bang?|Discard) Colon)? nameSpaceItem stringExpr? end;
exportStatement: Left_Arrow nameSpaceItem end;

namespaceSupportStatement:
importStatement |
namespaceFunctionStatement |
namespaceVariableStatement |
namespaceConstantStatement |
Expand All @@ -25,6 +23,11 @@ typeRedefineStatement |
typeTagStatement |
New_Line ;

// 导入命名空间
importStatement: Right_Arrow left_brace (importSubStatement|typeAliasStatement|New_Line)* right_brace end;

importSubStatement: (annotationSupport)? ((id Bang?|Discard) Colon)? nameSpaceItem stringExpr? end;

// 类型别名
typeAliasStatement: id Bang? Colon typeType end;
// 类型重定义
Expand Down
17 changes: 12 additions & 5 deletions Compiler/NameSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public partial class Namespace{
public partial class KLangVisitor{
public override object VisitStatement( StatementContext context ){
var obj = "";
var imports = "";
var ns = (Namespace)(Visit(context.exportStatement()));
obj+=(new System.Text.StringBuilder().Append("using Library;").Append(Wrap).Append("using static Library.Lib;").Append(Wrap)).to_str();
obj+=ns.imports+Wrap;
if ( context.annotationSupport()!=null ) {
obj+=Visit(context.annotationSupport());
}
Expand All @@ -44,6 +43,9 @@ public override object VisitStatement( StatementContext context ){
if ( type==typeof(NamespaceVariableStatementContext)||type==typeof(NamespaceFunctionStatementContext)||type==typeof(NamespaceConstantStatementContext) ) {
contentStatic+=Visit(item);
}
else if ( type==typeof(ImportStatementContext) ) {
imports+=Visit(item);
}
else {
content+=Visit(item);
}
Expand All @@ -54,18 +56,23 @@ public override object VisitStatement( StatementContext context ){
}
this.delete_current_set();
obj+=BlockRight+Wrap;
obj=(new System.Text.StringBuilder().Append("using Library;").Append(Wrap).Append("using static Library.Lib;").Append(Wrap).Append(imports).Append(Wrap)).to_str()+obj;
return obj;
}
public override object VisitExportStatement( ExportStatementContext context ){
var name = (string)(Visit(context.nameSpaceItem()));
var obj = (new Namespace(){name = name});
foreach (var item in context.importStatement()){
obj.imports+=(string)(Visit(item));
}
return obj;
}
public override object VisitImportStatement( ImportStatementContext context ){
var obj = "";
foreach (var item in context.importSubStatement()){
obj+=(string)(Visit(item));
}
return obj;
}
public override object VisitImportSubStatement( ImportSubStatementContext context ){
var obj = "";
if ( context.annotationSupport()!=null ) {
obj+=Visit(context.annotationSupport());
}
Expand Down
19 changes: 13 additions & 6 deletions Compiler/NameSpace.k
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ Namespace : $ {
KLangVisitor : $ me {
.VisitStatement : (context StatementContext -> v any) {
obj! : ""
imports! : ""
ns : Visit(context.exportStatement()) Namespace!
# "import library"
obj += "using Library;${Wrap}using static Library.Lib;${Wrap}"
obj += ns.imports + Wrap
? context.annotationSupport() >< nil {
obj += Visit(context.annotationSupport())
}
Expand Down Expand Up @@ -47,6 +45,8 @@ KLangVisitor : $ me {
type == typeof(NamespaceFunctionStatementContext) |
type == typeof(NamespaceConstantStatementContext) {
contentStatic += Visit(item)
} type == typeof(ImportStatementContext) {
imports += Visit(item)
} _ {
content += Visit(item)
}
Expand All @@ -57,6 +57,8 @@ KLangVisitor : $ me {
}
me.delete_current_set()
obj += BlockRight + Wrap
# "import library"
obj = "using Library;${Wrap}using static Library.Lib;${Wrap; imports; Wrap}" + obj
<- obj
}

Expand All @@ -65,13 +67,18 @@ KLangVisitor : $ me {
obj : Namespace{
name = name
}
@ item : context.importStatement() {
obj.imports += Visit(item) str!
}
<- obj
}

.VisitImportStatement : (context ImportStatementContext -> v any) {
obj! : ""
@ item : context.importSubStatement() {
obj += Visit(item) str!
}
<- obj
}

.VisitImportSubStatement : (context ImportSubStatementContext -> v any) {
obj! : ""
? context.annotationSupport() >< nil {
obj += Visit(context.annotationSupport())
Expand Down

0 comments on commit e89545e

Please sign in to comment.