forked from source-academy/js-slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `TypeLiteral` support in parser * feat: implement `make` builtin function Added ECE types for `TypeLiteral` and `make` related constructs * feat: implement abstraction for `Channel` Includes methods to `send` and `recv` * refactor: make use of getters and setters for `Channel` * refactor: force `GoStatement` to only take in `CallExpression` This is to temporarily fix the case where the parser mistakenly parse the a a call expression as a binary expression when a `GoStatement` is followed by a `ReceiveExpression` * chore: add doc to `make` (to note only buffered chans are supported now) * feat: add `SendStatement`, `ChanSendOp` and `ChanRecvOp` ECE types * feat: add `Channel` support in heap * feat: implement buffered channel support in ECE * refactor: implement circular queue for buffered channel * refactor: remove pushing `chanAddr` on recv/send retry * fix: expressions should not have optional line terminator in grammar Only a `ExpressionStatement` should look out for a line/file terminator
- Loading branch information
1 parent
ad70fb8
commit 0c73caf
Showing
9 changed files
with
504 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { WORD_SIZE } from './heap/config' | ||
|
||
/* prettier-ignore */ | ||
class Channel { | ||
protected memory: DataView | ||
|
||
constructor(memory: DataView) { this.memory = memory } | ||
|
||
protected get maxBufSize(): number { return this.memory.getUint16(5) } | ||
|
||
protected getSlotAddr(slotIdx: number): number { return (slotIdx + 1) * WORD_SIZE } | ||
|
||
protected getSlotValue(slotIdx: number): number { return this.memory.getFloat64(this.getSlotAddr(slotIdx)) } | ||
|
||
protected setSlotValue(slotIdx: number, value: number): void { this.memory.setFloat64(this.getSlotAddr(slotIdx), value) } | ||
} | ||
|
||
export class BufferedChannel extends Channel { | ||
static READ_IDX_OFFSET = 1 | ||
static WRITE_IDX_OFFSET = 2 | ||
static BUF_SIZE_OFFSET = 3 | ||
|
||
constructor(memory: DataView) { super(memory) } // prettier-ignore | ||
|
||
public send(value: any): boolean { | ||
if (this.isBufferFull()) { return false } // prettier-ignore | ||
|
||
// enqueue | ||
this.setSlotValue(this.writeIdx++ % this.maxBufSize, value) | ||
this.bufSize++ | ||
|
||
return true | ||
} | ||
|
||
public recv(): number | null { | ||
if (this.isBufferEmpty()) { return null } // prettier-ignore | ||
|
||
// dequeue | ||
const value = this.getSlotValue(this.readIdx++ % this.maxBufSize) | ||
this.bufSize-- | ||
|
||
return value | ||
} | ||
|
||
public isBufferFull(): boolean { | ||
return this.bufSize === this.maxBufSize | ||
} | ||
|
||
public isBufferEmpty(): boolean { | ||
return this.bufSize === 0 | ||
} | ||
|
||
private get readIdx(): number { | ||
return this.memory.getUint8(BufferedChannel.READ_IDX_OFFSET) | ||
} | ||
|
||
private set readIdx(newReadIdx: number) { | ||
this.memory.setUint8(BufferedChannel.READ_IDX_OFFSET, newReadIdx) | ||
} | ||
|
||
private get writeIdx(): number { | ||
return this.memory.getUint8(BufferedChannel.WRITE_IDX_OFFSET) | ||
} | ||
|
||
private set writeIdx(newWriteIdx: number) { | ||
this.memory.setUint8(BufferedChannel.WRITE_IDX_OFFSET, newWriteIdx) | ||
} | ||
|
||
private get bufSize(): number { | ||
return this.memory.getUint8(BufferedChannel.BUF_SIZE_OFFSET) | ||
} | ||
|
||
private set bufSize(newBufSize: number) { | ||
this.memory.setUint8(BufferedChannel.BUF_SIZE_OFFSET, newBufSize) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ export enum PointerTag { | |
BuiltInOp, | ||
ClosureOp, | ||
EnvOp, | ||
PopSOp | ||
PopSOp, | ||
BufferedChannel | ||
} |
Oops, something went wrong.