summaryrefslogtreecommitdiff
path: root/intellect-framework-from-internet/starts/bagel/nascent3.js
blob: 0a6c4a300dadaff2796e1330eebca6460ccdfb10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class ActiveMemory {
	constructor() {
		this.ram = []
		this.cloned = false
	}
	clone() {
		let ret = new ActiveMemory();
		ret.ram = this.ram
		ret.cloned = true
		return ret
	}
	add(...items) {
		if (this.cloned) { this.ram = this.ram.slice(); this.cloned = false; }
		for (let item of items)
			this.ram.push(item)
	}
	del(...items) {
		if (this.cloned) { this.ram = this.ram.slice(); this.cloned = false; }
		for (let item of items) {
			let index = this.ram.indexOf(item)
			this.ram.splice(index, 1)
		}
	}
	contains(...items) {
		for (let item of items)
			if (this.ram.indexOf(item) === -1) return false
		return true
	}
	containsWith(...props) {
		let ret = []
		outer: for (let a of this.ram) {
			for (let b of props)
				if (! a.has(b))
					continue outer
			return true
		}
		return false
	}
	getWith(...props) {
		let ret = []
		outer: for (let a of this.ram) {
			for (let b of props)
				if (! a.has(b))
					continue outer
			ret.push(a)
		}
		if (ret.length > 0) return ret
		return null
	}
}
class StringData extends String {
	constructor(str, from) {
		super(str)
		this.props = new Set(from && from.props)
		this.add('string')
		this.del('array')
	}
	add(val) {
		this.props.add(val)
	}
	has(val) {
		return this.props.has(val)
	}
	del(val) {
		this.props.delete(val)
	}
}
class ArrayData extends Array {
	constructor(arr, from) {
		super(...arr)
		this.props = new Set(from && from.props)
		this.add('array')
		this.del('string')
	}
	add(val) {
		this.props.add(val)
	}
	has(val) {
		return this.props.has(val)
	}
	del(val) {
		this.props.delete(val)
	}
}
class FunctionData {
	constructor(name, func, relevence, makes) {
		this.name = name
		this.props = new Set()
		this.props.add('function')
		this.props.add(name)
		this.call = func
		this.relevence = relevence
		this.makes = makes
	}
	add(val) {
		this.props.add(val)
	}
	has(val) {
		return this.props.has(val)
	}
	del(val) {
		this.props.delete(val)
	}
}

line2words = new FunctionData(
	'line2words',
	(line) => { // call
		let res = new ArrayData(line.split(' '), line)
		res.add('words')
		return res
	},
	(ram) => { // relevence
		return ram.getWith('text', 'string')
	},
	['array','words'] // makes
)

respondhello = new FunctionData(
	'respondhello',
	(words) => { // call
		console.log(words[0] + ", user!")
		let res = new StringData("said " + words[0])
		res.add('output')
		res.add('text')
		return res
	},
	(ram) => { // relevence
		let res = ram.getWith('words', 'input')
		if (res) res = res.filter(x => { x = x[0].toLowerCase(); return x == 'hello' || x == 'hi'})
		return res
	},
	['output', 'text'] // makes
)

// nodejs is missing a succinct read-line-from-stdin function.  make our own.
userinput = (() => {
	const readline = require('readline')
	const lines = []
	readline.createInterface({ input: process.stdin }).on('line', (line) => {
		lines.push(line)
	})

	return new FunctionData(
		'userinput',
		() => { // call
			let res = new StringData(lines.shift())
			res.add('text')
			res.add('input')
			return res
		},
		() => { // relevence
			return lines.length > 0
		},
		['string','text','input'] // makes
	)
})()


all_parts = [ line2words, respondhello ]

ram = new ActiveMemory()
optstried = new Set()
steps = []

var readline = require('readline')
readline.createInterface({
	input: process.stdin
}).on('line', (line) => {
	line = new StringData(line)
	line.add('input')
	line.add('text')
	ram.add(line)

	let cont
	do {
		cont = false
		for (let part of all_parts) {
			//console.log('for-loop-of-parts ' + part.name)
			let rel = part.relevence(ram)
			if (rel) {
				for (let a of rel) {
					//console.log('for-loop-of-rel ' + part.name + ' ' + a)
					if (optstried.has(part.name + ' ' + a)) continue; 
					//console.log('call-part ' + part.name + ' ' + a)
					res = part.call(a)
					ram.add(res)
					//console.log('part-called')
					step = [a, part.name, res]
					steps.push(step)
					console.log('made ' + step)
					cont = true
					optstried.add(part.name + ' ' + a)

				}
			}
		}
	} while (cont)
})