优化代码
This commit is contained in:
parent
78c18419ef
commit
d6cfa02e79
@ -14,4 +14,19 @@
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration default="false" name="SnowCompiler" type="Application" factoryName="Application" activateToolWindowBeforeRun="false" nameIsGenerated="true">
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="23" />
|
||||
<option name="MAIN_CLASS_NAME" value="org.jcnc.snow.compiler.cli.SnowCompiler" />
|
||||
<module name="SCompiler" />
|
||||
<option name="PROGRAM_PARAMETERS" value="test" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="org.jcnc.snow.compiler.parser.preprocessor.lexer.impl.api.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
@ -3,4 +3,8 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration default="false" name="build_project2tar.ps1" type="PowerShellRunType" factoryName="PowerShell" scriptUrl="$PROJECT_DIR$/build/build_project2tar.ps1" executablePath="C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe">
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@ -6,10 +6,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An intermediate-representation function (IRFunction).
|
||||
* An intermediate‑representation function (IRFunction).
|
||||
*
|
||||
* <p>For every source-level function we build one IRFunction that
|
||||
* records:</p>
|
||||
* <p>For every source‑level function we build one IRFunction that records:</p>
|
||||
* <ul>
|
||||
* <li>the function’s name,</li>
|
||||
* <li>a list of IR instructions (the <em>body</em>),</li>
|
||||
@ -27,12 +26,12 @@ public class IRFunction {
|
||||
/** linear list of IR instructions */
|
||||
private final List<IRInstruction> body = new ArrayList<>();
|
||||
|
||||
/* ---------- virtual-register management ---------- */
|
||||
/* ---------- virtual‑register management ---------- */
|
||||
|
||||
/** running counter for new virtual registers */
|
||||
private int regCounter = 0;
|
||||
|
||||
/** list of formal-parameter registers in declaration order */
|
||||
/** list of formal‑parameter registers in declaration order */
|
||||
private final List<IRVirtualRegister> parameters = new ArrayList<>();
|
||||
|
||||
/* ---------- construction ---------- */
|
||||
@ -43,7 +42,7 @@ public class IRFunction {
|
||||
|
||||
/* ---------- register helpers ---------- */
|
||||
|
||||
/** Allocates and returns a brand-new virtual register. */
|
||||
/** Allocates and returns a brand‑new virtual register. */
|
||||
public IRVirtualRegister newRegister() {
|
||||
return new IRVirtualRegister(regCounter++);
|
||||
}
|
||||
@ -51,7 +50,7 @@ public class IRFunction {
|
||||
/* ---------- parameter helpers ---------- */
|
||||
|
||||
/**
|
||||
* Adds a virtual register to the formal-parameter list.
|
||||
* Adds a virtual register to the formal‑parameter list.
|
||||
* Call this once for each parameter <em>in declaration order</em>
|
||||
* when building the IR.
|
||||
*/
|
||||
@ -79,9 +78,21 @@ public class IRFunction {
|
||||
|
||||
/* ---------- debugging ---------- */
|
||||
|
||||
@Override public String toString() {
|
||||
StringBuilder sb = new StringBuilder("func " + name + " {\n");
|
||||
@Override
|
||||
public String toString() {
|
||||
// Print function header with explicit parameter list: func name(%0, %1) {
|
||||
StringBuilder sb = new StringBuilder("func ")
|
||||
.append(name)
|
||||
.append('(');
|
||||
for (int i = 0; i < parameters.size(); i++) {
|
||||
sb.append(parameters.get(i));
|
||||
if (i < parameters.size() - 1) sb.append(", ");
|
||||
}
|
||||
sb.append(") {\n");
|
||||
|
||||
// Body instructions indented by two spaces
|
||||
body.forEach(i -> sb.append(" ").append(i).append('\n'));
|
||||
|
||||
return sb.append('}').toString();
|
||||
}
|
||||
}
|
||||
|
||||
14
test
14
test
@ -1,14 +1,12 @@
|
||||
module: CommonTasks
|
||||
function: main1
|
||||
function: main
|
||||
parameter:
|
||||
declare num1: int
|
||||
declare num2: int
|
||||
|
||||
return_type:int
|
||||
|
||||
body:
|
||||
num1 = 1
|
||||
num2=2
|
||||
return num1 + CommonTasks.test(3,4+1)
|
||||
|
||||
return CommonTasks.test(3,1)
|
||||
end body
|
||||
end function
|
||||
|
||||
@ -19,8 +17,8 @@ module: CommonTasks
|
||||
return_type:int
|
||||
body:
|
||||
declare result : int
|
||||
declare num3: int =4
|
||||
result = num1 + num2 +num3
|
||||
|
||||
result = num1 + num2
|
||||
return result
|
||||
end body
|
||||
end function
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user