Skip to content

Commit

Permalink
Catching expected return type mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Aug 18, 2023
1 parent bb5a762 commit 6ce65e1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface ASTResolveResults {

abstract class ASTNode {
public abstract resolve(
results: ASTResolveResults
results: ASTResolveResults,
returnType: DataType
): void;

public abstract marker(): Token;
Expand Down
38 changes: 26 additions & 12 deletions src/ast_expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class ExprASTBool implements ExpressionAST {
}

public resolve(
results: ASTResolveResults
results: ASTResolveResults,
returnType: DataType
): void { }

public marker(): Token {
Expand Down Expand Up @@ -83,7 +84,8 @@ class ExprASTString implements ExpressionAST {
}

public resolve(
results: ASTResolveResults
results: ASTResolveResults,
returnType: DataType
): void { }

public marker(): Token {
Expand Down Expand Up @@ -143,7 +145,10 @@ class ExprASTInt implements ExpressionAST {
);
}

public resolve(results: ASTResolveResults): void {
public resolve(
results: ASTResolveResults,
returnType: DataType
): void {
switch(this.bit) {
case 4:
this.minMaxFlow(
Expand Down Expand Up @@ -223,7 +228,10 @@ class ExprASTFloat implements ExpressionAST {
return LLVMDataType.getFloatDataType(this.bit);
}

public resolve(): void { }
public resolve(
results: ASTResolveResults,
returnType: DataType
): void { }

public marker(): Token {
return this.mark;
Expand Down Expand Up @@ -295,7 +303,10 @@ class ExprASTUnary implements ExpressionAST {
return this.expr.type();
}

public resolve(results: ASTResolveResults): void {
public resolve(
results: ASTResolveResults,
returnType: DataType
): void {
const dataType: DataType = this.expr.type();
const mrk: Token = this.marker();

Expand Down Expand Up @@ -416,7 +427,10 @@ class ExprASTEquality implements ExpressionAST {
return DataType.BOOL;
}

public resolve(results: ASTResolveResults): void {
public resolve(
results: ASTResolveResults,
returnType: DataType
): void {
const leftType: DataType =
this.left.type();
const rightType: DataType =
Expand Down Expand Up @@ -467,8 +481,8 @@ class ExprASTEquality implements ExpressionAST {
' is not allowed.'
);

this.left.resolve(results);
this.right.resolve(results);
this.left.resolve(results, returnType);
this.right.resolve(results, returnType);
}

public marker(): Token {
Expand Down Expand Up @@ -556,7 +570,8 @@ class ExprASTAndOr implements ExpressionAST {
}

public resolve(
results: ASTResolveResults
results: ASTResolveResults,
returnType: DataType
): void {
const leftType: DataType =
this.left.type();
Expand Down Expand Up @@ -600,9 +615,8 @@ class ExprASTAndOr implements ExpressionAST {
);
}


this.left.resolve(results);
this.right.resolve(results);
this.left.resolve(results, returnType);
this.right.resolve(results, returnType);
}

public marker(): Token {
Expand Down
43 changes: 35 additions & 8 deletions src/ast_stmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FunctionType,
ConstantInt,
Value,
ReturnInst
Type
} from "llvm-bindings";

import {
Expand Down Expand Up @@ -64,8 +64,14 @@ class StmtASTMain implements StatementAST {
}

public resolve(
results: ASTResolveResults
): void { }
results: ASTResolveResults,
returnType: DataType
): void {
this.body.resolve(
results,
returnType.getLLVMType() as unknown as DataType
);
}

public marker(): Token {
return this.mark;
Expand Down Expand Up @@ -115,8 +121,14 @@ class StmtASTRender implements StatementAST {
);
}

public resolve(results: ASTResolveResults): void {
this.expr.resolve(results);
public resolve(
results: ASTResolveResults,
returnType: DataType
): void {
this.expr.resolve(
results,
returnType
);
}

public marker(): Token {
Expand Down Expand Up @@ -154,10 +166,25 @@ class StmtASTReturn implements StatementAST {
}

public resolve(
results: ASTResolveResults
results: ASTResolveResults,
returnType: DataType
): void {
if(this.hasValue)
this.value?.resolve(results);
if(this.hasValue) {
const valueType: DataType =
this.value?.type() as DataType;

if(valueType != returnType)
results.errors.set(
this.mark,
'Invalid return type ' +
valueType.toString() + ' for type ' +
returnType.toString() + '.');

this.value?.resolve(
results,
returnType
);
}
}

public marker(): Token {
Expand Down
2 changes: 2 additions & 0 deletions src/data_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class DataType {

public static UNKNOWN: DataType =
new DataType("unknown", 0, undefined);
public static VOID: DataType =
new DataType("void", 0, Type.getVoidTy(LLVMGlobalContext));
public static STRING: DataType =
new DataType("string", 0, undefined);
public static BOOL: DataType =
Expand Down
16 changes: 11 additions & 5 deletions src/yttria.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
IRBuilder,
Module,
Type,
verifyModule
} from 'llvm-bindings';

Expand All @@ -17,11 +18,8 @@ import {
import {
ExprASTAndOr,
ExprASTBool,
ExprASTEquality,
ExprASTFloat,
ExprASTInt,
ExprASTString,
ExprASTUnary
ExprASTInt
} from './ast_expr';

import {
Expand All @@ -35,13 +33,16 @@ import colors from 'colors';
import LLVMGlobalContext from './llvm_context';
import yargs from 'yargs';
import YttriaUtil from './util';
import { File } from 'buffer';
import { DataType } from './data_type';

function tokenizerTest() {
var tokenizer = new Tokenizer(
'<anonymous>',
'#hello\nsub main\n0b0101011 0xcafebabe hh 3.14 "Hello\\tworld!" +++ <<< >>>'
);
var tokenizerResult: TokenizerResult = tokenizer.scan();
const outType: Type = Type.getVoidTy(LLVMGlobalContext)

if(tokenizerResult.tokens.length != 0) {
console.log('Tokens:');
Expand All @@ -68,9 +69,14 @@ function llvmTest() {

const expr1: ExprASTInt = new ExprASTInt(nullToken, BigInt('101'), 32);
const expr2: ExprASTInt = new ExprASTInt(nullToken, BigInt('99'), 32);

const cmp: ExprASTAndOr = new ExprASTAndOr(nullToken, '&', expr1, expr2);
const body: StmtASTRender = new StmtASTRender(nullToken, cmp);
const main: StmtASTMain = new StmtASTMain(nullToken, body);

const main: StmtASTMain = new StmtASTMain(
nullToken,
body
);

const builder: IRBuilder = new IRBuilder(LLVMGlobalContext);
main.visit(builder, module);
Expand Down

0 comments on commit 6ce65e1

Please sign in to comment.