Skip to content

Commit

Permalink
Fix parenthesis handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic Barker committed Feb 3, 2022
1 parent 5ad2d72 commit 854ea44
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ It's starting to look a lot more like a programming language. You can see that w

![Jan-22-2022 13-14-00](https://user-images.githubusercontent.com/2264338/150615797-6b079b06-9186-4209-bc70-66d169fc1921.gif)


### Running your code

You can run your .rvr code in two ways:
Expand All @@ -234,6 +233,7 @@ You can run your .rvr code in two ways:
- The `Assembly` tab provides live output of the corresponding assembly for your chosen target architecture and platform. At the moment river compiles to [nasm](https://www.nasm.us/) assembly, but I'm not sure if it'll stay like that in future.

Currently supported platforms:

- x64 (Windows)
- x64 (Mac OS X)
- WebAssembly
Expand Down
24 changes: 21 additions & 3 deletions src/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function App() {
"build" | "asm" | "macros"
>("asm");
const [instructionRange, setInstructionRange] = useState<[number, number]>([
0, 0,
0,
0,
]);
const [macrosExpanded, setMacrosExpanded] = useState(false);
const [dismissMap] = useState<DismissMap>([]);
Expand Down Expand Up @@ -146,7 +147,7 @@ export function App() {
onClick={() => {
openFiles.splice(selectedFileIndex + 1, 0, {
name: e.name.toLocaleLowerCase().replaceAll(" ", "_") + ".rvr",
instructions: [{ type: "emptyInstruction", fragments: [undefined] }],
instructions: parseTextFile(e.file),
});
setOpenFiles(openFiles.slice());
setSelectedFileIndex(selectedFileIndex + 1);
Expand Down Expand Up @@ -280,7 +281,24 @@ export function App() {
{activeRightTab === "macros" && (
<div className="macros">
<div className="header subheader">
<button className="subheaderButton">New Macro</button>
<button
className="subheaderButton"
onClick={() => {
macros.push({
name: "New Macro",
instructions: [
{
type: "emptyInstruction",
fragments: [undefined],
},
],
inline: false,
});
setMacros(macros.slice());
}}
>
New Macro
</button>
</div>
{macrosRendered}
</div>
Expand Down
20 changes: 19 additions & 1 deletion src/compiler/x64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ export function compileX64(
switch (instruction.action) {
case "=":
case "+":
case "-": {
case "-":
case "&&":
case "||": {
operands = [target, source];
break;
}
Expand Down Expand Up @@ -583,6 +585,22 @@ export function compileX64(
]);
break;
}
case "&&": {
instructionOutputs[1].push([
,
"and",
formatOp(operands[0], operands[1]),
]);
break;
}
case "||": {
instructionOutputs[1].push([
,
"or",
formatOp(operands[0], operands[1]),
]);
break;
}
default:
break;
}
Expand Down
76 changes: 17 additions & 59 deletions src/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ function renderFragments(
}
} else if (fragment?.type === "instruction" && fragment.value === "_") {
fragmentContent = "_block";
} else if (fragment?.type === "block") {
fragmentContent = fragment.value === "open" ? "{" : "}";
} else if (fragment) {
fragmentContent = fragment.value;
} else if (cursorPos === i && hasCursor) {
Expand Down Expand Up @@ -242,7 +244,6 @@ function renderInstructions(
let instructionsRendered: React.ReactNode[] = [];
let blockRanges: [number, number][] = [];
for (let li = 0; li < instructions.length; li++) {
const previousInstruction = li > 0 ? instructions[li - 1] : undefined;
const instruction = instructions[li];
const fragments = renderFragments(
instruction,
Expand All @@ -261,64 +262,28 @@ function renderInstructions(
blockRanges = blockRanges.concat(instruction.blockRanges);
}

let preLine: React.ReactNode = null;

if (
instruction.type === "scopeInstruction" &&
instruction.fragments[1]?.value === "close"
(instruction.type === "scopeInstruction" &&
instruction.fragments[1]?.value === "close") ||
(instruction.type === "blockInstruction" &&
instruction.fragments[0]?.value === "close")
) {
indent -= 2;
}

if (
blockRanges.find((br) => br[0] <= instruction.lineNumber) &&
(!previousInstruction ||
blockRanges.find((br) => br[0] > previousInstruction.lineNumber))
) {
indent += 2;
preLine = <div>{"{"}</div>;
}

let indentRendered = Array(Math.max(indent, 0))
.fill(0)
.map(() => <div className="indent"> </div>);

let braceIndentRendered = Array(Math.max(indent - 2, 0))
.fill(0)
.map(() => <div className="indent"> </div>);

if (
instruction.type === "scopeInstruction" &&
instruction.fragments[1]?.value === "open"
(instruction.type === "scopeInstruction" &&
instruction.fragments[1]?.value === "open") ||
(instruction.type === "blockInstruction" &&
instruction.fragments[0]?.value === "open")
) {
indent += 2;
}

// [index, brace]
let postLine: [React.ReactNode, React.ReactNode][] = [];
if (blockRanges.find((br) => br[1] - 1 === instruction.lineNumber)) {
indent -= 2;
const braceIndent = Array(Math.max(indent, 0))
.fill(0)
.map(() => <div className="indent"> </div>);
postLine.push([braceIndent, <div>{"}"}</div>]);
}

if (
blockRanges.find(
(br) =>
li < instructions.length - 1 &&
br[1] - 1 > instruction.lineNumber &&
br[1] - 1 < instructions[li + 1].lineNumber
)
) {
indent -= 2;
const braceIndent = Array(Math.max(indent, 0))
.fill(0)
.map(() => <div className="indent"> </div>);
postLine.push([braceIndent, <div>{"}"}</div>]);
}

let contents: React.ReactElement;

if (
Expand Down Expand Up @@ -364,12 +329,6 @@ function renderInstructions(
}

instructionsRendered.push([
preLine && (
<div className="macro-block-braces">
{braceIndentRendered}
{preLine}
</div>
),
<div
className={classnames("line", {
selected:
Expand All @@ -379,15 +338,13 @@ function renderInstructions(
})}
key={li}
>
<div className="lineNumber">{instruction.lineNumber + 1}</div>
<div className="lineNumber">
{instruction.type === "blockInstruction"
? ""
: instruction.lineNumber + 1}
</div>
<div className="instruction">{contents}</div>
</div>,
postLine.map((l) => (
<div className="macro-block-braces">
{l[0]}
{l[1]}
</div>
)),
]);
}
return instructionsRendered;
Expand Down Expand Up @@ -421,7 +378,8 @@ export function Editor({
const isMacro = !!sourceMacro;
const [cursorPositions, setCursorPositions] = useState([0]);
const [selectionRange, setSelectionRange] = useState<[number, number]>([
-1, -1,
-1,
-1,
]);
const [instructionIndex, setInstructionIndex] = useState(0);
const [macroSearchString, setMacroSearchString] = useState<
Expand Down
Loading

1 comment on commit 854ea44

@vercel
Copy link

@vercel vercel bot commented on 854ea44 Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.