wire up comments to ui (#11)

This commit is contained in:
Kurt Hutten
2023-01-24 21:09:00 +11:00
committed by GitHub
parent dd5022b38e
commit bdf778530f
4 changed files with 39 additions and 11 deletions

View File

@ -7,7 +7,8 @@ import {
} from './lang/abstractSyntaxTree'
import { ProgramMemory, Position, PathToNode, Rotation } from './lang/executor'
import { recast } from './lang/recast'
import { lexer } from './lang/tokeniser'
import { lexer, Token } from './lang/tokeniser'
import { processTokens } from './lang/recast'
export type Range = [number, number]
@ -63,7 +64,7 @@ interface StoreState {
addLog: (log: string) => void
resetLogs: () => void
ast: Program | null
setAst: (ast: Program | null) => void
setAst: (ast: Program | null, tokens?: Token[]) => void
updateAst: (ast: Program, focusPath?: PathToNode) => void
code: string
setCode: (code: string) => void
@ -75,6 +76,7 @@ interface StoreState {
setError: (error?: string) => void
programMemory: ProgramMemory
setProgramMemory: (programMemory: ProgramMemory) => void
tokens: Token[]
}
export const useStore = create<StoreState>()((set, get) => ({
@ -119,11 +121,16 @@ export const useStore = create<StoreState>()((set, get) => ({
set({ logs: [] })
},
ast: null,
setAst: (ast) => {
set({ ast })
setAst: (ast, tokens) => {
if (tokens) {
set({ tokens: processTokens(tokens), ast })
} else {
set({ ast, tokens: [] })
}
},
updateAst: (ast, focusPath) => {
const newCode = recast(ast)
const tokens = get().tokens
const newCode = recast(ast, tokens)
const astWithUpdatedSource = abstractSyntaxTree(lexer(newCode))
set({ ast: astWithUpdatedSource, code: newCode })
@ -142,8 +149,9 @@ export const useStore = create<StoreState>()((set, get) => ({
},
formatCode: () => {
const code = get().code
const ast = abstractSyntaxTree(lexer(code))
const newCode = recast(ast)
const tokens = lexer(code)
const ast = abstractSyntaxTree(tokens)
const newCode = recast(ast, processTokens(tokens))
set({ code: newCode, ast })
},
errorState: {
@ -155,4 +163,5 @@ export const useStore = create<StoreState>()((set, get) => ({
},
programMemory: { root: {}, _sketch: [] },
setProgramMemory: (programMemory) => set({ programMemory }),
tokens: [],
}))