Skip to content

Commit

Permalink
Add conditional expressions to compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
benmurphyy committed Jan 31, 2024
1 parent f5c7805 commit a6f5a8f
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 48 deletions.
4 changes: 3 additions & 1 deletion ctowasm/src/parser/c-ast/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import FunctionDefinition from "~src/parser/c-ast/functionDefinition";
import Block from "~src/parser/c-ast/statement/compoundStatement";
import IdentifierExpression from "~src/parser/c-ast/expression/identifierExpr";
import { ModuleName } from "~src/modules";
import ConditionalExpression from "~src/parser/c-ast/expression/conditionalExpression";

export interface CNodeBase {
type: string;
Expand Down Expand Up @@ -49,7 +50,8 @@ export type Expression =
| Constant
| IdentifierExpression
| UnaryExpression
| CommaSeparatedExpressions;
| CommaSeparatedExpressions
| ConditionalExpression;

export interface CommaSeparatedExpressions extends CNodeBase {
type: "CommaSeparatedExpressions";
Expand Down
5 changes: 4 additions & 1 deletion ctowasm/src/processor/c-ast/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ScalarCDataType } from "~src/common/types";
import { ConstantP } from "~src/processor/c-ast/expression/constants";
import {
BinaryExpressionP,
ConditionalExpressionP,
PostStatementExpressionP,
PreStatementExpressionP,
UnaryExpressionP,
Expand All @@ -23,6 +24,7 @@ import { SelectionStatementP } from "~src/processor/c-ast/statement/selectionSta
import { JumpStatementP } from "~src/processor/c-ast/statement/jumpStatement";
import { PrimaryDataTypeMemoryObjectDetails } from "~src/processor/dataTypeUtil";
import { ModuleName } from "~src/modules";
import ConditionalExpression from "~src/parser/c-ast/expression/conditionalExpression";

export type CNodeP = FunctionDefinitionP | StatementP | ExpressionP;

Expand All @@ -49,7 +51,8 @@ export type ExpressionP =
| PostStatementExpressionP
| UnaryExpressionP
| Address
| MemoryLoad;
| MemoryLoad
| ConditionalExpressionP;

/**
* All expressions should inherit this, as all expressions should have a primary data type.
Expand Down
19 changes: 10 additions & 9 deletions ctowasm/src/processor/c-ast/expression/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ export interface UnaryExpressionP extends ExpressionPBase {
expr: ExpressionP;
}

/**
* Wrapper node that indicates that the wrapped expression is to be treated as a boolean (int that is 1 or 0)
*/
export interface BooleanExpressionP extends ExpressionPBase {
type: "BooleanExpression";
expr: ExpressionP;
dataType: "signed int";
}

/**
* Represents an expression that consists of a series of statements followed by a expression
* Used as the result of processing assignment expressions.
Expand All @@ -49,6 +40,16 @@ export interface PostStatementExpressionP extends ExpressionPBase {
expr: ExpressionP;
}

/**
* Represents a inline conditional expression e.g: 1 ? 2 : 3
*/
export interface ConditionalExpressionP extends ExpressionPBase {
type: "ConditionalExpression";
condition: ExpressionP;
trueExpression: ExpressionP; // expression to return if condition is not zero (true)
falseExpression: ExpressionP;
}

/**
* A wrapper for the result of processing expressions, to be used by the processor only (not present in generated AST)
*/
Expand Down
10 changes: 10 additions & 0 deletions ctowasm/src/processor/expressionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ export function checkBinaryExpressionDataTypesValidity(
}
}

/**
* Determine the overall datatype of a ConditionalExpression (e.g. 1 ? 2 : 3).
* Follows same rules as binary expressions ("+" used as placeholder).
*/
export function determineConditionalExpressionDataType(leftExprDataType: ScalarDataType,
rightExprDataType: ScalarDataType) {
const dataType = determineResultDataTypeOfBinaryExpression(leftExprDataType, rightExprDataType, "+");
return dataType.type === "pointer" ? "pointer" : dataType.primaryDataType;
}

/**
* Determines the type that operands in a binary expression should be converted to before the operation,
* according to rules of arithemetic conversion 6.3.1.8 in C17 standard.
Expand Down
Loading

0 comments on commit a6f5a8f

Please sign in to comment.