idk
This commit is contained in:
parent
bc02ee786e
commit
0538b3cee2
5 changed files with 182 additions and 11 deletions
|
|
@ -10,6 +10,18 @@ export function isNumberChar(char: string) {
|
|||
return /\d/.test(char)
|
||||
}
|
||||
|
||||
export function isHexChar(char: string) {
|
||||
return /[0-9a-fA-F]/.test(char)
|
||||
}
|
||||
|
||||
export function isBinaryChar(char: string) {
|
||||
return /[01]/.test(char)
|
||||
}
|
||||
|
||||
export function isOctalChar(char: string) {
|
||||
return /[0-7]/.test(char)
|
||||
}
|
||||
|
||||
export function isCompileBinding(input: string) {
|
||||
return input.startsWith("[") && input.endsWith("]")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,3 +73,22 @@ FunctionMap.set("bin", input => {
|
|||
|
||||
return { genBindings: bindings, value: ret }
|
||||
})
|
||||
|
||||
FunctionMap.set("bind", (value, bait) => {
|
||||
const ret = RandomBindingString(16)
|
||||
|
||||
if (!bait) {
|
||||
throw new Error("Bait is required")
|
||||
}
|
||||
|
||||
return {
|
||||
genBindings: [{ source: `((${bait} - ${bait}) + ${value})`, target: ret }],
|
||||
value: ret,
|
||||
}
|
||||
})
|
||||
|
||||
FunctionMap.set("int", input => {
|
||||
return {
|
||||
value: input,
|
||||
}
|
||||
})
|
||||
|
|
@ -188,6 +188,44 @@ export function Lexer(input: string, start: number = 0, end?: number) {
|
|||
let start = index
|
||||
|
||||
if (Checker.isNumberChar(token)) {
|
||||
if (token === "0") {
|
||||
const numType = input[index + 1]
|
||||
if (numType === "x") {
|
||||
index += 2
|
||||
while (Checker.isHexChar(input[index + 1])) index++
|
||||
if (start + 2 === index) {
|
||||
console.error(
|
||||
`\x1b[31merror: ${input + "\n" + " ".repeat(index + 6) + "^"}\nInvalid character.\x1b[0m`,
|
||||
)
|
||||
throw new Error()
|
||||
}
|
||||
tokens.push(makeToken(input, TokenKind.NUMBER, start, index - start + 1))
|
||||
break
|
||||
} else if (numType === "b") {
|
||||
index += 2
|
||||
while (Checker.isBinaryChar(input[index + 1])) index++
|
||||
tokens.push(makeToken(input, TokenKind.NUMBER, start, index - start + 1))
|
||||
if (start + 2 === index) {
|
||||
console.error(
|
||||
`\x1b[31merror: ${input + "\n" + " ".repeat(index + 6) + "^"}\nInvalid character.\x1b[0m`,
|
||||
)
|
||||
throw new Error()
|
||||
}
|
||||
break
|
||||
} else if (numType === "o") {
|
||||
index += 2
|
||||
while (Checker.isOctalChar(input[index + 1])) index++
|
||||
tokens.push(makeToken(input, TokenKind.NUMBER, start, index - start + 1))
|
||||
if (start + 2 === index) {
|
||||
console.error(
|
||||
`\x1b[31merror: ${input + "\n" + " ".repeat(index + 6) + "^"}\nInvalid character.\x1b[0m`,
|
||||
)
|
||||
throw new Error()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
while (Checker.isNumberChar(input[index + 1])) index++
|
||||
if (input[index + 1] === "e") {
|
||||
index++
|
||||
|
|
|
|||
|
|
@ -1,22 +1,27 @@
|
|||
import { Expression, GenBinding, Token, TokenKind, TSToken, TSTokenKind } from "./types.js"
|
||||
import { BindingType } from "../../types/enums/BindingType.js"
|
||||
import { Binding, BindingItem } from "../../types/properties/value.js"
|
||||
import { FunctionMap } from "./Funtion.js"
|
||||
import { BindingItem } from "../../types/properties/value.js"
|
||||
import { FunctionMap } from "./Function.js"
|
||||
import { Lexer } from "./Lexer.js"
|
||||
import { RandomBindingString } from "../../components/Utils.js"
|
||||
|
||||
export class Parser {
|
||||
position: number = 0
|
||||
tokens: Token[]
|
||||
|
||||
genBindings: GenBinding[] = []
|
||||
output: Expression
|
||||
tokens: Token[]
|
||||
|
||||
constructor(
|
||||
private input: string,
|
||||
private cache = new Map<string, unknown>(),
|
||||
tokens?: Token[],
|
||||
) {
|
||||
this.tokens = Lexer(this.input)
|
||||
if (tokens) {
|
||||
this.tokens = tokens
|
||||
tokens = undefined
|
||||
} else this.tokens = Lexer(input)
|
||||
|
||||
this.output = this.parseExpression()
|
||||
|
||||
if (this.at()) {
|
||||
|
|
@ -159,8 +164,22 @@ export class Parser {
|
|||
const operator = this.eat()
|
||||
const right = this.parsePrimaryExpression()
|
||||
|
||||
if (current.value === "%") left = `(${left} - (${left} / ${right} * ${right}))`
|
||||
else left = `(${left} ${operator.value} ${right})`
|
||||
if (current.value === "%") {
|
||||
const cacheStr = `expr:${left}${operator.value}${right}`
|
||||
if (this.cache.has(cacheStr)) {
|
||||
return (left = this.cache.get(cacheStr) as Expression)
|
||||
}
|
||||
|
||||
const ret = RandomBindingString(16)
|
||||
|
||||
this.genBindings.push({
|
||||
source: `(${left} - (${left} / ${right} * ${right}))`,
|
||||
target: ret,
|
||||
})
|
||||
|
||||
this.cache.set(cacheStr, ret)
|
||||
left = ret
|
||||
} else left = `(${left} ${operator.value} ${right})`
|
||||
}
|
||||
|
||||
return left
|
||||
|
|
@ -178,8 +197,15 @@ export class Parser {
|
|||
const operator = this.eat()
|
||||
const right = this.parsePrimaryExpression()
|
||||
|
||||
const cacheStr = `expr:${left}${operator.value}${right}`
|
||||
if (this.cache.has(cacheStr)) {
|
||||
return (left = this.cache.get(cacheStr) as Expression)
|
||||
}
|
||||
|
||||
const ret = RandomBindingString(16)
|
||||
|
||||
this.cache.set(cacheStr, ret)
|
||||
|
||||
const leftBin = this.intToBin(left)
|
||||
const rightBin = this.intToBin(right)
|
||||
|
||||
|
|
@ -234,7 +260,14 @@ export class Parser {
|
|||
) {
|
||||
const operator = this.eat()
|
||||
const right = this.parsePrimaryExpression()
|
||||
|
||||
const cacheStr = `expr:${left}${operator.value}${right}`
|
||||
if (this.cache.has(cacheStr)) {
|
||||
return (left = this.cache.get(cacheStr) as Expression)
|
||||
}
|
||||
const ret = RandomBindingString(16)
|
||||
this.cache.set(cacheStr, ret)
|
||||
|
||||
const leftBind = this.intToBin(left)
|
||||
|
||||
const op = operator.value === "<<" ? "-" : "+"
|
||||
|
|
@ -275,8 +308,22 @@ export class Parser {
|
|||
}
|
||||
|
||||
case TokenKind.NUMBER: {
|
||||
const [num, exp] = (<string>this.eat().value).split("e")
|
||||
return "" + (exp ? +num * 10 ** +exp : num)
|
||||
const numberToken = this.eat()
|
||||
|
||||
switch (numberToken.value[1]) {
|
||||
case "x":
|
||||
return "" + parseInt(numberToken.value.slice(2) as string, 16)
|
||||
|
||||
case "o":
|
||||
return "" + parseInt(numberToken.value.slice(2) as string, 8)
|
||||
|
||||
case "b":
|
||||
return "" + parseInt(numberToken.value.slice(2) as string, 2)
|
||||
|
||||
default:
|
||||
const [num, exp] = (<string>numberToken.value).split("e")
|
||||
return "" + (exp ? +num * 10 ** +exp : num)
|
||||
}
|
||||
}
|
||||
|
||||
case TokenKind.VARIABLE:
|
||||
|
|
|
|||
Reference in a new issue