Kaynağa Gözat

- Added values for default arguments to the CallExpression

- Started work on definition formatting
Stan Hebben 6 yıl önce
ebeveyn
işleme
8c090f75c1

+ 71
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/DefinitionFormatter.java Dosyayı Görüntüle

1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.formatter;
7
+
8
+import org.openzen.zenscript.codemodel.definition.AliasDefinition;
9
+import org.openzen.zenscript.codemodel.definition.ClassDefinition;
10
+import org.openzen.zenscript.codemodel.definition.DefinitionVisitor;
11
+import org.openzen.zenscript.codemodel.definition.EnumDefinition;
12
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
13
+import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
14
+import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
15
+import org.openzen.zenscript.codemodel.definition.StructDefinition;
16
+
17
+/**
18
+ *
19
+ * @author Hoofdgebruiker
20
+ */
21
+public class DefinitionFormatter implements DefinitionVisitor<Void> {
22
+	private final FormattingSettings settings;
23
+	private final TypeFormatter typeFormatter;
24
+	private final StringBuilder builder = new StringBuilder();
25
+	private final String indent;
26
+	
27
+	public DefinitionFormatter(FormattingSettings settings, TypeFormatter typeFormatter, String indent) {
28
+		this.settings = settings;
29
+		this.typeFormatter = typeFormatter;
30
+		this.indent = indent;
31
+	}
32
+
33
+	@Override
34
+	public Void visitClass(ClassDefinition definition) {
35
+		return null;
36
+	}
37
+
38
+	@Override
39
+	public Void visitInterface(InterfaceDefinition definition) {
40
+		return null;
41
+	}
42
+
43
+	@Override
44
+	public Void visitEnum(EnumDefinition definition) {
45
+		return null;
46
+	}
47
+
48
+	@Override
49
+	public Void visitStruct(StructDefinition definition) {
50
+		return null;
51
+	}
52
+
53
+	@Override
54
+	public Void visitFunction(FunctionDefinition definition) {
55
+		return null;
56
+	}
57
+
58
+	@Override
59
+	public Void visitExpansion(ExpansionDefinition definition) {
60
+		return null;
61
+	}
62
+
63
+	@Override
64
+	public Void visitAlias(AliasDefinition definition) {
65
+		return null;
66
+	}
67
+	
68
+	public String toString() {
69
+		return builder.toString();
70
+	}
71
+}

+ 12
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FileFormatter.java Dosyayı Görüntüle

34
 		TypeFormatter typeFormatter = new TypeFormatter(settings, importer);
34
 		TypeFormatter typeFormatter = new TypeFormatter(settings, importer);
35
 		ExpressionFormatter expressionFormatter = new ExpressionFormatter(settings, typeFormatter);
35
 		ExpressionFormatter expressionFormatter = new ExpressionFormatter(settings, typeFormatter);
36
 		
36
 		
37
+		List<DefinitionFormatter> definitionFormatters = new ArrayList<>();
38
+		for (HighLevelDefinition definition : definitions) {
39
+			DefinitionFormatter definitionFormatter = new DefinitionFormatter(settings, typeFormatter, "");
40
+			definition.accept(definitionFormatter);
41
+			definitionFormatters.add(definitionFormatter);
42
+		}
43
+		
37
 		StatementFormatter scriptFormatter = new StatementFormatter("", settings, expressionFormatter);
44
 		StatementFormatter scriptFormatter = new StatementFormatter("", settings, expressionFormatter);
38
 		for (Statement statement : script.statements) {
45
 		for (Statement statement : script.statements) {
39
 			statement.accept(scriptFormatter);
46
 			statement.accept(scriptFormatter);
41
 		
48
 		
42
 		StringBuilder output = new StringBuilder();
49
 		StringBuilder output = new StringBuilder();
43
 		importer.write(output);
50
 		importer.write(output);
51
+		
52
+		for (DefinitionFormatter definition : definitionFormatters) {
53
+			output.append(definition.toString());
54
+		}
55
+		
44
 		output.append(scriptFormatter.toString().trim());
56
 		output.append(scriptFormatter.toString().trim());
45
 		
57
 		
46
 		WhitespacePostComment postComment = script.getTag(WhitespacePostComment.class);
58
 		WhitespacePostComment postComment = script.getTag(WhitespacePostComment.class);

+ 13
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallExpression.java Dosyayı Görüntüle

5
  */
5
  */
6
 package org.openzen.zenscript.codemodel.expression;
6
 package org.openzen.zenscript.codemodel.expression;
7
 
7
 
8
+import java.util.Arrays;
8
 import org.openzen.zenscript.codemodel.FunctionHeader;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
9
 import org.openzen.zenscript.codemodel.member.FunctionalMember;
10
 import org.openzen.zenscript.codemodel.member.FunctionalMember;
10
 import org.openzen.zenscript.shared.CodePosition;
11
 import org.openzen.zenscript.shared.CodePosition;
12
+import org.openzen.zenscript.shared.CompileException;
13
+import org.openzen.zenscript.shared.CompileExceptionCode;
11
 
14
 
12
 /**
15
 /**
13
  *
16
  *
21
 	public CallExpression(CodePosition position, Expression target, FunctionalMember member, FunctionHeader instancedHeader, CallArguments arguments) {
24
 	public CallExpression(CodePosition position, Expression target, FunctionalMember member, FunctionHeader instancedHeader, CallArguments arguments) {
22
 		super(position, instancedHeader.returnType);
25
 		super(position, instancedHeader.returnType);
23
 		
26
 		
27
+		if (arguments.arguments.length < instancedHeader.parameters.length) {
28
+			Expression[] newArguments = Arrays.copyOf(arguments.arguments, instancedHeader.parameters.length);
29
+			for (int i = arguments.arguments.length; i < instancedHeader.parameters.length; i++) {
30
+				if (instancedHeader.parameters[i].defaultValue == null)
31
+					throw new CompileException(position, CompileExceptionCode.MISSING_PARAMETER, "Parameter missing and no default value specified");
32
+				newArguments[i] = instancedHeader.parameters[i].defaultValue;
33
+			}
34
+			arguments = new CallArguments(arguments.typeArguments, newArguments);
35
+		}
36
+		
24
 		this.target = target;
37
 		this.target = target;
25
 		this.member = member;
38
 		this.member = member;
26
 		this.arguments = arguments;
39
 		this.arguments = arguments;

+ 0
- 1
Linker/src/main/java/org/openzen/zenscript/linker/StatementScope.java Dosyayı Görüntüle

11
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
11
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
12
 import org.openzen.zenscript.codemodel.statement.VarStatement;
12
 import org.openzen.zenscript.codemodel.statement.VarStatement;
13
 import org.openzen.zenscript.codemodel.type.GenericName;
13
 import org.openzen.zenscript.codemodel.type.GenericName;
14
-import org.openzen.zenscript.linker.BaseScope;
15
 import org.openzen.zenscript.shared.CodePosition;
14
 import org.openzen.zenscript.shared.CodePosition;
16
 
15
 
17
 /**
16
 /**

+ 1
- 0
Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java Dosyayı Görüntüle

139
 	
139
 	
140
 	public void listDefinitions(PackageDefinitions definitions) {
140
 	public void listDefinitions(PackageDefinitions definitions) {
141
 		for (ParsedDefinition definition : this.definitions) {
141
 		for (ParsedDefinition definition : this.definitions) {
142
+			definition.getCompiled().setTag(SourceFile.class, new SourceFile(filename));
142
 			definitions.add(definition.getCompiled());
143
 			definitions.add(definition.getCompiled());
143
 			definition.linkInnerTypes();
144
 			definition.linkInnerTypes();
144
 		}
145
 		}

+ 25
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/FileContents.java Dosyayı Görüntüle

1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.scriptingexample;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
+import org.openzen.zenscript.codemodel.ScriptBlock;
12
+
13
+/**
14
+ *
15
+ * @author Hoofdgebruiker
16
+ */
17
+public class FileContents {
18
+	public final String filename;
19
+	public ScriptBlock script;
20
+	public final List<HighLevelDefinition> definitions = new ArrayList<>();
21
+	
22
+	public FileContents(String filename) {
23
+		this.filename = filename;
24
+	}
25
+}

+ 37
- 6
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java Dosyayı Görüntüle

4
 import java.io.IOException;
4
 import java.io.IOException;
5
 import java.util.ArrayList;
5
 import java.util.ArrayList;
6
 import java.util.Collections;
6
 import java.util.Collections;
7
+import java.util.HashMap;
7
 import java.util.List;
8
 import java.util.List;
8
 import java.util.Map;
9
 import java.util.Map;
9
 import java.util.Optional;
10
 import java.util.Optional;
40
 		GlobalRegistry registry = new GlobalRegistry(global);
41
 		GlobalRegistry registry = new GlobalRegistry(global);
41
 		SemanticModule module = compileSyntaxToSemantic(parsedFiles, registry);
42
 		SemanticModule module = compileSyntaxToSemantic(parsedFiles, registry);
42
 		
43
 		
43
-		FormattingSettings settings = new FormattingSettings.Builder().build();
44
-		for (ScriptBlock block : module.scripts) {
45
-			FileFormatter formatter = new FileFormatter(settings);
46
-			System.out.println("== " + block.getTag(SourceFile.class).filename + " ==");
47
-			System.out.println(formatter.format(pkg, block, Collections.emptyList()));
48
-		}
44
+		formatFiles(pkg, module);
49
 		
45
 		
50
 		if (module.isValid) {
46
 		if (module.isValid) {
51
 			JavaModule javaModule = compileSemanticToJava(module);
47
 			JavaModule javaModule = compileSemanticToJava(module);
63
 		return parsedFiles;
59
 		return parsedFiles;
64
 	}
60
 	}
65
 	
61
 	
62
+	private static void formatFiles(ZSPackage pkg, SemanticModule module) {
63
+		Map<String, FileContents> files = new HashMap<>();
64
+		for (ScriptBlock block : module.scripts) {
65
+			SourceFile file = block.getTag(SourceFile.class);
66
+			if (file == null)
67
+				continue;
68
+			
69
+			FileContents contents = new FileContents(file.filename);
70
+			contents.script = block;
71
+			files.put(file.filename, contents);
72
+		}
73
+		
74
+		for (HighLevelDefinition definition : module.definitions.getAll()) {
75
+			SourceFile file = definition.getTag(SourceFile.class);
76
+			if (file == null)
77
+				continue;
78
+			
79
+			if (!files.containsKey(file.filename))
80
+				files.put(file.filename, new FileContents(file.filename));
81
+			
82
+			files.get(file.filename).definitions.add(definition);
83
+		}
84
+		
85
+		List<String> filenames = new ArrayList<>(files.keySet());
86
+		Collections.sort(filenames);
87
+		
88
+		FormattingSettings settings = new FormattingSettings.Builder().build();
89
+		for (String filename : filenames) {
90
+			FileContents contents = files.get(filename);
91
+			FileFormatter formatter = new FileFormatter(settings);
92
+			System.out.println("== " + filename + " ==");
93
+			System.out.println(formatter.format(pkg, contents.script, contents.definitions));
94
+		}
95
+	}
96
+	
66
 	private static SemanticModule compileSyntaxToSemantic(ParsedFile[] files, GlobalRegistry registry) {
97
 	private static SemanticModule compileSyntaxToSemantic(ParsedFile[] files, GlobalRegistry registry) {
67
 		// We are considering all these files to be in the same package, so make
98
 		// We are considering all these files to be in the same package, so make
68
 		// a single PackageDefinition instance. If these files were in multiple
99
 		// a single PackageDefinition instance. If these files were in multiple

+ 2
- 1
Shared/src/main/java/org/openzen/zenscript/shared/CompileExceptionCode.java Dosyayı Görüntüle

56
 	RETURN_VALUE_VOID,
56
 	RETURN_VALUE_VOID,
57
 	INVALID_CONDITION,
57
 	INVALID_CONDITION,
58
 	INTERNAL_ERROR,
58
 	INTERNAL_ERROR,
59
-	CANNOT_SET_FINAL_VARIABLE
59
+	CANNOT_SET_FINAL_VARIABLE,
60
+	MISSING_PARAMETER
60
 }
61
 }

Loading…
İptal
Kaydet