Skip to content

Commit

Permalink
Revert "Add Source §2 (#35)"
Browse files Browse the repository at this point in the history
This reverts commit 86eb90a.
  • Loading branch information
ning-y authored May 26, 2018
1 parent 86eb90a commit def9246
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 150 deletions.
2 changes: 0 additions & 2 deletions src/actions/actionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export const CLEAR_REPL_INPUT = 'CLEAR_REPL_INPUT'
export const CLEAR_REPL_OUTPUT = 'CLEAR_REPL_OUTPUT'
export const CLEAR_CONTEXT = 'CLEAR_CONTEXT'
export const SEND_REPL_INPUT_TO_OUTPUT = 'SEND_REPL_INPUT_TO_OUTPUT'
export const CHAPTER_SELECT = 'CHAPTER_SELECT'
export const CHANGE_CHAPTER = 'CHANGE_CHAPTER'

/** Interpreter */
export const HANDLE_CONSOLE_LOG = 'HANDLE_CONSOLE_LOG'
Expand Down
13 changes: 0 additions & 13 deletions src/actions/playground.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChangeEvent } from 'react'
import { ActionCreator } from 'redux'
import * as actionTypes from './actionTypes'

Expand All @@ -20,18 +19,6 @@ export const sendReplInputToOutput: ActionCreator<actionTypes.IAction> = (newOut
}
})

export const chapterSelect: ActionCreator<actionTypes.IAction> = (
e: ChangeEvent<HTMLSelectElement>
) => ({
type: actionTypes.CHAPTER_SELECT,
payload: e.currentTarget.value
})

export const changeChapter: ActionCreator<actionTypes.IAction> = (newChapter: number) => ({
type: actionTypes.CHANGE_CHAPTER,
payload: newChapter
})

export const evalEditor = () => ({
type: actionTypes.EVAL_EDITOR
})
Expand Down
54 changes: 19 additions & 35 deletions src/components/IDE/Control.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Button, IconName, Intent } from '@blueprintjs/core'
import * as React from 'react'

import { sourceChapters } from '../../reducers/states'
import { Button, IconName, Intent } from '@blueprintjs/core'

/**
* @property handleEvalEditor - A callback function for evaluation
Expand All @@ -11,40 +10,26 @@ export interface IControlProps {
handleEvalEditor: () => void
handleEvalRepl: () => void
handleClearReplOutput: () => void
handleChapterSelect: (e: React.ChangeEvent<HTMLSelectElement>) => void
}

const genericButton = (
label: string,
icon: IconName,
handleClick = () => {},
intent = Intent.NONE,
notMinimal = false
) => (
<Button
onClick={handleClick}
className={(notMinimal ? '' : 'pt-minimal') + ' col-xs-12'}
intent={intent}
icon={icon}
>
{label}
</Button>
)

const chapterSelect = (handleSelect = (e: React.ChangeEvent<HTMLSelectElement>) => {}) => (
<div className="col-xs-4 pt-select pt-select">
<select defaultValue={sourceChapters.slice(-1)[0].toString()} onChange={handleSelect}>
{sourceChapters.map(chap => (
<option key={chap} value={chap}>
{`Source \xa7${chap}`}
</option>
))}
</select>
</div>
)

class Control extends React.Component<IControlProps, {}> {
public render() {
const genericButton = (
label: string,
icon: IconName,
handleClick = () => {},
intent = Intent.NONE,
notMinimal = false
) => (
<Button
onClick={handleClick}
className={(notMinimal ? '' : 'pt-minimal') + ' col-xs-12'}
intent={intent}
icon={icon}
>
{label}
</Button>
)
const runButton = genericButton('Run', 'play', this.props.handleEvalEditor)
const evalButton = genericButton('Eval', 'play', this.props.handleEvalRepl)
const clearButton = genericButton('Clear', 'remove', this.props.handleClearReplOutput)
Expand All @@ -53,9 +38,8 @@ class Control extends React.Component<IControlProps, {}> {
<div className="col-xs-2">{runButton}</div>
<div className="col-xs-4">
<div className="row">
{chapterSelect(this.props.handleChapterSelect)}
<div className="col-xs-4">{evalButton}</div>
<div className="col-xs-4">{clearButton}</div>
<div className="col-xs-6">{evalButton}</div>
<div className="col-xs-6">{clearButton}</div>
</div>
</div>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/components/IDE/__tests__/Control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ test('Control renders correctly', () => {
const props: IControlProps = {
handleEvalEditor: () => {},
handleEvalRepl: () => {},
handleClearReplOutput: () => {},
handleChapterSelect: (e: React.ChangeEvent<HTMLSelectElement>) => {}
handleClearReplOutput: () => {}
}
const app = <Control {...props} />
const tree = shallow(app)
Expand Down
14 changes: 2 additions & 12 deletions src/components/IDE/__tests__/__snapshots__/Control.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,12 @@ exports[`Control renders correctly 1`] = `
</div>
<div className=\\"col-xs-4\\">
<div className=\\"row\\">
<div className=\\"col-xs-4 pt-select pt-select\\">
<select defaultValue=\\"2\\" onChange={[Function: handleChapterSelect]}>
<option value={1}>
Source §1
</option>
<option value={2}>
Source §2
</option>
</select>
</div>
<div className=\\"col-xs-4\\">
<div className=\\"col-xs-6\\">
<Blueprint2.Button onClick={[Function: handleEvalRepl]} className=\\"pt-minimal col-xs-12\\" intent=\\"none\\" icon=\\"play\\">
Eval
</Blueprint2.Button>
</div>
<div className=\\"col-xs-4\\">
<div className=\\"col-xs-6\\">
<Blueprint2.Button onClick={[Function: handleClearReplOutput]} className=\\"pt-minimal col-xs-12\\" intent=\\"none\\" icon=\\"remove\\">
Clear
</Blueprint2.Button>
Expand Down
8 changes: 3 additions & 5 deletions src/containers/IDE/ControlContainer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'
import { bindActionCreators, Dispatch } from 'redux'

import { chapterSelect, clearReplOutput, evalEditor, evalRepl } from '../../actions/playground'
import { clearReplOutput, evalEditor, evalRepl } from '../../actions/playground'
import Control, { IControlProps } from '../../components/IDE/Control'
import { IState } from '../../reducers/states'

type DispatchProps = Pick<IControlProps, 'handleEvalEditor'> &
Pick<IControlProps, 'handleEvalRepl'> &
Pick<IControlProps, 'handleClearReplOutput'> &
Pick<IControlProps, 'handleChapterSelect'>
Pick<IControlProps, 'handleClearReplOutput'>

/** No-op mapStateToProps */
const mapStateToProps: MapStateToProps<{}, {}, IState> = state => ({})
Expand All @@ -21,8 +20,7 @@ const mapDispatchToProps: MapDispatchToProps<DispatchProps, {}> = (dispatch: Dis
{
handleEvalEditor: evalEditor,
handleEvalRepl: evalRepl,
handleClearReplOutput: clearReplOutput,
handleChapterSelect: chapterSelect
handleClearReplOutput: clearReplOutput
},
dispatch
)
Expand Down
4 changes: 2 additions & 2 deletions src/mocks/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext } from '../slang'
import { Context } from '../slang/types'

export function mockContext(chapter = 1): Context {
return createContext(chapter)
export function mockContext(): Context {
return createContext()
}
8 changes: 1 addition & 7 deletions src/reducers/playground.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Reducer } from 'redux'
import {
CHANGE_CHAPTER,
CLEAR_CONTEXT,
CLEAR_REPL_INPUT,
CLEAR_REPL_OUTPUT,
Expand Down Expand Up @@ -42,12 +41,7 @@ export const reducer: Reducer<IPlaygroundState> = (state = defaultPlayground, ac
case CLEAR_CONTEXT:
return {
...state,
context: createContext(state.sourceChapter)
}
case CHANGE_CHAPTER:
return {
...state,
sourceChapter: action.payload
context: createContext()
}
case HANDLE_CONSOLE_LOG:
/* Possible cases:
Expand Down
7 changes: 1 addition & 6 deletions src/reducers/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ export interface IApplicationState {
readonly environment: ApplicationEnvironment
}

export const sourceChapters = [1, 2]
const latestSourceChapter = sourceChapters.slice(-1)[0]

export interface IPlaygroundState {
readonly sourceChapter: number
readonly editorValue: string
readonly replValue: string
readonly context: Context
Expand Down Expand Up @@ -91,9 +87,8 @@ export const defaultApplication: IApplicationState = {
}

export const defaultPlayground: IPlaygroundState = {
sourceChapter: latestSourceChapter,
editorValue: '',
replValue: '',
context: createContext(latestSourceChapter),
context: createContext(),
output: []
}
15 changes: 0 additions & 15 deletions src/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Context, interrupt, runInContext } from '../slang'

import * as actions from '../actions'
import * as actionTypes from '../actions/actionTypes'
import { showSuccessMessage } from '../notification'

function* evalCode(code: string, context: Context) {
const { result, interrupted } = yield race({
Expand Down Expand Up @@ -45,20 +44,6 @@ function* interpreterSaga(): SagaIterator {
yield put(actions.sendReplInputToOutput(code))
yield* evalCode(code, context)
})

yield takeEvery(actionTypes.CHAPTER_SELECT, function*(action) {
const newChapter = parseInt((action as actionTypes.IAction).payload, 10)
const oldChapter = yield select((state: IState) => state.playground.sourceChapter)
if (newChapter !== oldChapter) {
yield put(actions.changeChapter(newChapter))
yield put(actions.handleInterruptExecution())
yield put(actions.clearContext())
yield put(actions.clearReplOutput())
yield call(showSuccessMessage, `Switched to Source \xa7${newChapter}`)
} else {
yield undefined
}
})
}

function* mainSaga() {
Expand Down
48 changes: 25 additions & 23 deletions src/slang/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const createEmptyRuntime = () => ({
nodes: []
})

export const createEmptyContext = (chapter: number): Context => ({
chapter,
export const createEmptyContext = (week: number): Context => ({
week,
errors: [],
cfg: createEmptyCFG(),
runtime: createEmptyRuntime()
Expand Down Expand Up @@ -56,7 +56,7 @@ export const importExternals = (context: Context, externals: string[]) => {
export const importBuiltins = (context: Context) => {
ensureGlobalEnvironmentExist(context)

if (context.chapter >= 1) {
if (context.week >= 3) {
defineSymbol(context, 'runtime', misc.runtime)
defineSymbol(context, 'display', misc.display)
defineSymbol(context, 'error', misc.error_message)
Expand All @@ -79,15 +79,22 @@ export const importBuiltins = (context: Context) => {
}
}

if (context.chapter >= 2) {
// List library
if (context.week >= 4) {
defineSymbol(context, 'math_log', Math.log)
defineSymbol(context, 'math_exp', Math.exp)
defineSymbol(context, 'alert', alert)
defineSymbol(context, 'math_floor', Math.floor)
defineSymbol(context, 'timed', misc.timed)
}

if (context.week >= 5) {
defineSymbol(context, 'list', list.list)
defineSymbol(context, 'pair', list.pair)
defineSymbol(context, 'is_pair', list.is_pair)
defineSymbol(context, 'is_list', list.is_list)
defineSymbol(context, 'is_empty_list', list.is_empty_list)
defineSymbol(context, 'head', list.head)
defineSymbol(context, 'tail', list.tail)
defineSymbol(context, 'is_empty_list', list.is_empty_list)
defineSymbol(context, 'is_list', list.is_list)
defineSymbol(context, 'list', list.list)
defineSymbol(context, 'length', list.length)
defineSymbol(context, 'map', list.map)
defineSymbol(context, 'build_list', list.build_list)
Expand All @@ -98,40 +105,35 @@ export const importBuiltins = (context: Context) => {
defineSymbol(context, 'member', list.member)
defineSymbol(context, 'remove', list.remove)
defineSymbol(context, 'remove_all', list.remove_all)
defineSymbol(context, 'equal', list.equal)
defineSymbol(context, 'assoc', list.assoc)
defineSymbol(context, 'filter', list.filter)
defineSymbol(context, 'enum_list', list.enum_list)
defineSymbol(context, 'list_ref', list.list_ref)
defineSymbol(context, 'accumulate', list.accumulate)
defineSymbol(context, 'equal', list.equal)
}

if (context.chapter >= Infinity) {
// previously week 4
defineSymbol(context, 'alert', alert)
defineSymbol(context, 'math_floor', Math.floor)
defineSymbol(context, 'timed', misc.timed)
// previously week 5
defineSymbol(context, 'assoc', list.assoc)
if (window.hasOwnProperty('ListVisualizer')) {
defineSymbol(context, 'draw', (window as any).ListVisualizer.draw)
} else {
defineSymbol(context, 'draw', () => {
throw new Error('List visualizer is not enabled')
})
}
// previously week 6
}
if (context.week >= 6) {
defineSymbol(context, 'is_number', misc.is_number)
// previously week 8
}
if (context.week >= 8) {
defineSymbol(context, 'undefined', undefined)
defineSymbol(context, 'set_head', list.set_head)
defineSymbol(context, 'set_tail', list.set_tail)
// previously week 9
}
if (context.week >= 9) {
defineSymbol(context, 'array_length', misc.array_length)
}
}

const createContext = (chapter = 1, externals = []) => {
const context = createEmptyContext(chapter)
const createContext = (week = 3, externals = []) => {
const context = createEmptyContext(week)

importBuiltins(context)
importExternals(context, externals)
Expand Down
4 changes: 2 additions & 2 deletions src/slang/interop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generate } from 'astring'
import { MAX_LIST_DISPLAY_LENGTH } from './constants'
import { apply } from './interpreter'
import { ArrowClosure, Closure, Context, Value } from './types'
import { Closure, Context, Value } from './types'

export const closureToJS = (value: Value, context: Context, klass: string) => {
function DummyClass(this: Value) {
Expand Down Expand Up @@ -30,7 +30,7 @@ export const closureToJS = (value: Value, context: Context, klass: string) => {
}

export const toJS = (value: Value, context: Context, klass?: string) => {
if (value instanceof Closure || value instanceof ArrowClosure) {
if (value instanceof Closure) {
return value.fun
} else {
return value
Expand Down
6 changes: 3 additions & 3 deletions src/slang/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Context, ErrorSeverity, ErrorType, SourceError } from './types'

// tslint:disable-next-line:interface-name
export interface ParserOptions {
chapter: number
week: number
}

export class DisallowedConstructError implements SourceError {
Expand Down Expand Up @@ -133,7 +133,7 @@ for (const type of Object.keys(syntaxTypes)) {
usages: []
}
context.cfg.edges[id] = []
if (syntaxTypes[node.type] > context.chapter) {
if (syntaxTypes[node.type] > context.week) {
context.errors.push(new DisallowedConstructError(node))
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ rules.forEach(rule => {
const keys = Object.keys(rule.checkers)
keys.forEach(key => {
walkers[key] = compose(walkers[key], (node, context) => {
if (typeof rule.disableOn !== 'undefined' && context.chapter >= rule.disableOn) {
if (typeof rule.disableOn !== 'undefined' && context.week >= rule.disableOn) {
return
}
const checker = rule.checkers[key]
Expand Down
Loading

0 comments on commit def9246

Please sign in to comment.