Skip to content

Commit

Permalink
Merged branch 'feature/fields' into 'master'.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktMagnus committed Mar 7, 2024
2 parents d852047 + c460438 commit 8818985
Show file tree
Hide file tree
Showing 122 changed files with 2,894 additions and 2,694 deletions.
4 changes: 4 additions & 0 deletions examples/classes/main.ph
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ function main ()
MyClass.sayHelloFromFunction();

let myClass := new MyClass();

myClass.sayHelloFromMethod();

myClass.setHelloField('Still hello from a method!');
myClass.sayHelloFromMethod();
}
9 changes: 8 additions & 1 deletion examples/classes/myClass.ph
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import Standard.Io;

class ClassesExample.MyClass;

field variable currentHello: String := 'Hello from method!';

function sayHelloFromFunction ()
{
Io.writeLine('Hello from function!');
}

method sayHelloFromMethod ()
{
Io.writeLine('Hello from method!');
Io.writeLine(currentHello);
}

method setHelloField (value: String)
{
currentHello := value;
}
4 changes: 2 additions & 2 deletions examples/modules/myModule.ph
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Standard.Io;

module ModulesExample.MyModule;

constant helloFromModule := 'Hello got from MyModule!';
variable sayHelloCounter := 0;
constant helloFromModule: String := 'Hello got from MyModule!';
variable sayHelloCounter: Int := 0;

function getHello (): String
{
Expand Down
13 changes: 13 additions & 0 deletions examples/variables/main.ph
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Standard.Io;
import VariableExample.MyClass;

module VariableExample.Main;

function main ()
{
Io.writeLine(MyClass.doFunctionThings());

let myClass := new MyClass();

Io.writeLine(myClass.doMethodThings());
}
22 changes: 22 additions & 0 deletions examples/variables/variableExample.ph
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class VariableExample.MyClass;

constant moduleConstant: String := 'moduleConstant';
variable moduleVariable: String := 'moduleVariable';

field fieldConstant: String := 'fieldConstant';
field variable fieldVariable: String := 'fieldVariable';

function doFunctionThings (): String
{
let localConstant := moduleVariable;

return localConstant;
}

method doMethodThings (): String
{
let variable localVariable := fieldVariable;
localVariable := fieldConstant;

return localVariable;
}
59 changes: 20 additions & 39 deletions src/compilerInterface/phoshorCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as Diagnostic from '../diagnostic';
import { Connector } from '../connector/connector';
import { FileIntermediate } from '../intermediateLowerer/intermediates/fileIntermediate';
import { File as FileSemanticNode } from '../connector/semanticNodes';
import { FileSyntaxNode } from '../parser/syntaxNodes/fileSyntaxNode';
import FileSystem from 'fs';
import { Importer } from '../importer/importer';
import { IntermediateInterpreter } from '../interpreter/intermediateInterpreter';
import { IntermediateLowerer } from '../intermediateLowerer/intermediateLowerer';
import { Lexer } from '../lexer/lexer';
import { LinuxAmd64Backend } from '../backends/linuxAmd64Backend';
import { IntermediateLowerer } from '../intermediateLowerer/intermediateLowerer';
import { ModuleSemanticSymbol } from '../connector/semanticSymbols/moduleSemanticSymbol';
import { Parser } from '../parser/parser';
import Path from 'path';
Expand Down Expand Up @@ -108,7 +106,7 @@ export class PhosphorCompiler
{
const fileSemanticTree = connector.run(syntaxTree, qualifiedNameToFile);
// TODO: Check if the qualified name is already in the map.
qualifiedNameToFile.set(fileSemanticTree.module.qualifiedName, fileSemanticTree);
qualifiedNameToFile.set(fileSemanticTree.module.namespace.qualifiedName, fileSemanticTree);

if ((fileSemanticTree.variables.length > 0) && !fileSemanticTree.module.isEntryPoint)
{
Expand All @@ -126,48 +124,31 @@ export class PhosphorCompiler

// TODO: Check if the exit codes of assemblers/linkers/compilers in the backends is non-zero in case of errors.

if (processArguments.run)
const objectFiles: string[] = [];
for (const [qualifiedName, fileSemanticTree] of qualifiedNameToFile)
{
const intermediateFiles: FileIntermediate[] = [];
for (const [, fileSemanticTree] of qualifiedNameToFile)
const loweredTree = semanticLowerer.run(fileSemanticTree, modulesWithInitialisers);
const intermediateLanguage = intermediateLowerer.run(loweredTree);

if (processArguments.intermediate)
{
const loweredTree = semanticLowerer.run(fileSemanticTree);
const intermediateLanguage = intermediateLowerer.run(loweredTree, modulesWithInitialisers);
intermediateFiles.push(intermediateLanguage);
const intermediateTranspiler = new TranspilerIntermediate();
const intermediateCode = intermediateTranspiler.run(intermediateLanguage);

FileSystem.writeFileSync(
Path.join(processArguments.temporaryPath, qualifiedName + '.phi'),
intermediateCode,
{ encoding: 'utf8' }
);
}

const intermediateInterpreter = new IntermediateInterpreter();

intermediateInterpreter.run(intermediateFiles);
const objectFilePath = backend.compile(intermediateLanguage, qualifiedName, processArguments.temporaryPath);
objectFiles.push(objectFilePath);
}
else
{
const objectFiles: string[] = [];
for (const [qualifiedName, fileSemanticTree] of qualifiedNameToFile)
{
const loweredTree = semanticLowerer.run(fileSemanticTree);
const intermediateLanguage = intermediateLowerer.run(loweredTree, modulesWithInitialisers);

if (processArguments.intermediate)
{
const intermediateTranspiler = new TranspilerIntermediate();
const intermediateCode = intermediateTranspiler.run(intermediateLanguage);

FileSystem.writeFileSync(
Path.join(processArguments.temporaryPath, qualifiedName + '.phi'),
intermediateCode,
{ encoding: 'utf8' }
);
}

const objectFilePath = backend.compile(intermediateLanguage, qualifiedName, processArguments.temporaryPath);
objectFiles.push(objectFilePath);
}

const standardLibraryFilePath = Path.join(standardLibraryTargetPath, 'standardLibrary.a');
const standardLibraryFilePath = Path.join(standardLibraryTargetPath, 'standardLibrary.a');

backend.link(objectFiles, standardLibraryFilePath, processArguments.outputPath);
}
backend.link(objectFiles, standardLibraryFilePath, processArguments.outputPath);
}

private getSourceFilesInPathRecursively (directoryPath: string): string[]
Expand Down
8 changes: 0 additions & 8 deletions src/compilerInterface/processArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface OptionValues
temporaryPath?: string;
optimisation?: OptimisationLevel;
target?: TargetPlatform;
run?: boolean;
intermediate?: boolean;
}

Expand All @@ -25,7 +24,6 @@ export class ProcessArguments
public readonly temporaryPath: string;
public readonly optimisationLevel: OptimisationLevel;
public readonly targetPlatform: TargetPlatform;
public readonly run: boolean;
public readonly intermediate: boolean;

constructor (argv?: string[])
Expand Down Expand Up @@ -90,11 +88,6 @@ export class ProcessArguments
targetOption.choices(Object.values(TargetPlatform));
command.addOption(targetOption);

const runOption = new Option('-r, --run', 'Run the programme with the interpreter instead of compiling it');
command.addOption(runOption);
/* TODO: Instead of an optional parameter it should be either necessary to specify the run option or output file and
standard library. */

const intermediateOption = new Option('-i, --intermediate', 'Generate intermediate code');
command.addOption(intermediateOption);

Expand All @@ -113,7 +106,6 @@ export class ProcessArguments
this.temporaryPath = options.temporaryPath ?? 'tmp';
this.optimisationLevel = options.optimisation ?? OptimisationLevel.None;
this.targetPlatform = options.target ?? TargetPlatform.LinuxAmd64; // TODO: Use the platform the compiler runs on as default.
this.run = options.run ?? false;
this.intermediate = options.intermediate ?? false;
}
}
Loading

0 comments on commit 8818985

Please sign in to comment.