diff --git a/.gitignore b/.gitignore index 76a1c5d..c2658d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -dist/ node_modules/ -bundle/ diff --git a/dist/index.min.js b/dist/index.min.js new file mode 100644 index 0000000..2a7fc1d --- /dev/null +++ b/dist/index.min.js @@ -0,0 +1 @@ +(()=>{"use strict";var e={404:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{constructor(e=null){this.superEnvironment=e,this.table=new Map}get(e){const t=this.table.get(e);return void 0!==t?t:null===this.superEnvironment?null:this.superEnvironment.get(e)}set(e,t){this.table.set(e,t)}}},290:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.wrapReturnValue=t.makeEvaluatedEmpty=t.makeEvaluatedFunction=t.makeEvaluatedBoolean=t.makeEvaluatedString=t.makeEvaluatedNumber=void 0,t.makeEvaluatedNumber=e=>({type:"number",value:e,get representation(){return`${e}`}}),t.makeEvaluatedString=e=>({type:"string",value:e,get representation(){return`'${e}'`}}),t.makeEvaluatedBoolean=e=>({type:"boolean",value:e,get representation(){return e?"참":"거짓"}}),t.makeEvaluatedFunction=(e,t,r)=>({type:"function",parameters:e,body:t,environment:r,get representation(){return"(함수)"}}),t.makeEvaluatedEmpty=()=>({type:"empty",get representation(){return"(비어있음)"}}),t.wrapReturnValue=e=>({type:"return value",value:e})},673:function(e,t,r){var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.Environment=void 0;const n=r(290),i=a(r(404));t.default=class{evaluate(e,t){return this.evaluateProgram(e,t)}evaluateProgram(e,t){const{statements:r}=e;if(0===r.length)return(0,n.makeEvaluatedEmpty)();for(let e=0;e"===r)return(0,n.makeEvaluatedBoolean)(a.value>i.value);if("<"===r)return(0,n.makeEvaluatedBoolean)(a.value="===r)return(0,n.makeEvaluatedBoolean)(a.value>=i.value);if("<="===r)return(0,n.makeEvaluatedBoolean)(a.value<=i.value)}if("number"===a.type&&"number"===i.type){if("+"===r)return(0,n.makeEvaluatedNumber)(a.value+i.value);if("-"===r)return(0,n.makeEvaluatedNumber)(a.value-i.value);if("*"===r)return(0,n.makeEvaluatedNumber)(a.value*i.value);if("/"===r)return(0,n.makeEvaluatedNumber)(a.value/i.value);throw new Error(`bad infix ${r} for number operands`)}throw new Error(`bad infix ${r}, with left '${a}' and right '${i}'`)}parseCallArguments(e,t){const r=[];for(const a of e){const e=this.evaluateExpression(a,t);r.push(e)}return r}evaluateFunctionCall(e,t){const r=new i.default(e.environment);for(let a=0;a{const t=new u.default(e),r=new o.default(t).parseProgram(),a=new l.default,n=new l.Environment,i=a.evaluate(r,n);return String(i.representation)}},197:function(e,t,r){var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const n=a(r(13));class i{constructor(e){this.reader=new n.default(e,i.END_OF_INPUT)}pop(){const e=this.reader.read();return this.reader.next(),e}peek(){return this.reader.read()}}i.END_OF_INPUT="\0",t.default=i},13:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{constructor(e,t){this.chars=e,this.fallbackChar=t,this.index=0}read(){return this.index===this.chars.length?this.fallbackChar:this.chars[this.index]}next(){this.index!==this.chars.length&&this.index++}}},439:function(e,t,r){var a=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,n)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&a(t,e,r);return n(t,e),t},s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const u=s(r(197)),o=i(r(632)),l=i(r(352));t.default=class{constructor(e){this.charBuffer=new u.default(e)}getToken(){this.skipWhitespaces();const e=this.charBuffer.peek();switch(e){case"+":case"-":case"*":case"/":{const e=this.charBuffer.pop();return o.operator(e)}case"(":case")":{const e=this.charBuffer.pop();return o.groupDelimiter(e)}case"{":case"}":{const e=this.charBuffer.pop();return o.blockDelimiter(e)}case",":{const e=this.charBuffer.pop();return o.separator(e)}case"!":{this.charBuffer.pop();const e=this.readOperatorStartingWithBang();return o.operator(e)}case"=":{this.charBuffer.pop();const e=this.readOperatorStartingWithEqual();return o.operator(e)}case">":{this.charBuffer.pop();const e=this.readOperatorStartingWithGreaterThan();return o.operator(e)}case"<":{this.charBuffer.pop();const e=this.readOperatorStartingWithLessThan();return o.operator(e)}case"'":{this.charBuffer.pop();const[e,t]=this.readStringLiteral();return t?o.stringLiteral(e):o.illegal("'"+e)}case u.default.END_OF_INPUT:return o.end;default:if(l.isDigit(e)){const e=this.readNumberLiteral();return o.numberLiteral(e)}if(l.isLetter(e)){const e=this.readLettersAndDigits();return"참"===e||"거짓"===e?o.booleanLiteral(e):"만약"===e||"아니면"===e||"함수"===e||"결과"===e?o.keyword(e):o.identifier(e)}return this.charBuffer.pop(),o.illegal(e)}}skipWhitespaces(){for(;l.isWhitespace(this.charBuffer.peek());)this.charBuffer.pop()}readOperatorStartingWithBang(){return"="===this.charBuffer.peek()?(this.charBuffer.pop(),"!="):"!"}readOperatorStartingWithEqual(){return"="===this.charBuffer.peek()?(this.charBuffer.pop(),"=="):"="}readOperatorStartingWithGreaterThan(){return"="===this.charBuffer.peek()?(this.charBuffer.pop(),">="):">"}readOperatorStartingWithLessThan(){return"="===this.charBuffer.peek()?(this.charBuffer.pop(),"<="):"<"}readStringLiteral(){const e=[];for(;;){const t=this.charBuffer.pop();if("'"===t||t===u.default.END_OF_INPUT)return[e.join(""),"'"===t];e.push(t)}}readNumberLiteral(){return this.readDigits()+this.readDecimalPart()}readDigits(){const e=[];for(;l.isDigit(this.charBuffer.peek());)e.push(this.charBuffer.pop());return e.join("")}readDecimalPart(){return"."!==this.charBuffer.peek()?"":this.charBuffer.pop()+this.readDigits()}readLettersAndDigits(){const e=[];for(;l.isLetter(this.charBuffer.peek())||l.isDigit(this.charBuffer.peek());)e.push(this.charBuffer.pop());return e.join("")}}},632:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.end=t.illegal=t.keyword=t.separator=t.blockDelimiter=t.groupDelimiter=t.stringLiteral=t.booleanLiteral=t.numberLiteral=t.identifier=t.operator=t.END_VALUE=void 0,t.END_VALUE="$end",t.operator=e=>({type:"operator",value:e}),t.identifier=e=>({type:"identifier",value:e}),t.numberLiteral=e=>({type:"number literal",value:e}),t.booleanLiteral=e=>({type:"boolean literal",value:e}),t.stringLiteral=e=>({type:"string literal",value:e}),t.groupDelimiter=e=>({type:"group delimiter",value:e}),t.blockDelimiter=e=>({type:"block delimiter",value:e}),t.separator=e=>({type:"separator",value:e}),t.keyword=e=>({type:"keyword",value:e}),t.illegal=e=>({type:"illegal",value:e}),t.end={type:"end",value:t.END_VALUE}},352:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isWhitespace=t.isDigit=t.isLetter=void 0,t.isLetter=e=>1===e.length&&/^[a-zA-Z가-힣_]$/.test(e),t.isDigit=e=>1===e.length&&/^[0-9]$/.test(e),t.isWhitespace=e=>1===e.length&&/^[ \t\r\n]$/.test(e)},522:function(e,t,r){var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const n=r(763),i=a(r(461)),s=e=>{switch(e){case"=":return 30;case"==":case"!=":case">":case"<":case">=":case"<=":return 40;case"+":case"-":return 50;case"*":case"/":return 60;case"(":return 80;default:return 0}};t.default=class{constructor(e){this.buffer=new i.default(e)}parseProgram(){const e=(0,n.makeProgram)();for(;!this.buffer.isEnd();){const t=this.parseStatement();null!==t&&e.statements.push(t)}return e}parseBlock(){const e=this.buffer.read();if("block delimiter"!==e.type||"{"!==e.value)throw new Error(`expected { but received ${e.type}`);this.buffer.next();const t=[];for(;;){const e=this.buffer.read();if("block delimiter"===e.type&&"}"===e.value){this.buffer.next();break}const r=this.parseStatement();null!==r&&t.push(r)}return(0,n.makeBlock)(t)}parseStatement(){const e=this.buffer.read();return"keyword"===e.type&&"만약"===e.value?(this.buffer.next(),this.parseBranchStatement()):"keyword"===e.type&&"결과"===e.value?(this.buffer.next(),this.parseReturnStatement()):this.parseExpressionStatement()}parseBranchStatement(){const e=this.parseExpression(0),t=this.parseBlock(),r=this.buffer.read();if("keyword"!==r.type||"아니면"!==r.value)return(0,n.makeBranchStatement)(e,t);this.buffer.next();const a=this.parseBlock();return(0,n.makeBranchStatement)(e,t,a)}parseReturnStatement(){const e=this.parseExpression(0);return(0,n.makeReturnStatement)(e)}parseExpressionStatement(){const e=this.parseExpression(0);return(0,n.makeExpressionStatement)(e)}parseExpression(e){let t=this.parsePrefixExpression();for(;!(s(this.buffer.read().value)<=e);){const e=this.parseInfixExpression(t);if(null===e)break;t=e}return t}parsePrefixExpression(){const e=this.buffer.read();if(this.buffer.next(),"number literal"===e.type)return this.parseNumberLiteral(e.value);if("boolean literal"===e.type)return this.parseBooleanLiteral(e.value);if("string literal"===e.type)return this.parseStringLiteral(e.value);if("identifier"===e.type)return(0,n.makeIdentifier)(e.value);if("operator"===e.type&&("+"===e.value||"-"===e.value||"!"===e.value)){const t=this.parseExpression(70),r=e.value;return(0,n.makePrefixExpression)(r,t)}if("keyword"===e.type&&"함수"===e.value){const e=this.parseParameters(),t=this.parseBlock();return(0,n.makeFunctionExpression)(t,e)}if("group delimiter"===e.type&&"("===e.value){const e=this.parseExpression(0),t=this.buffer.read();if(this.buffer.next(),"group delimiter"!==t.type||")"!==t.value)throw new Error(`expected ) but received ${t.type}`);return e}throw new Error(`bad token type ${e.type} (${e.value}) for prefix expression`)}parseParameters(){const e=[],t=this.buffer.read();if("group delimiter"!==t.type||"("!==t.value)throw new Error(`expected ( but received ${t.type}`);this.buffer.next();const r=this.buffer.read();if(this.buffer.next(),"group delimiter"===r.type&&")"===r.value)return[];const a=r;if("identifier"!==a.type)throw new Error(`expected identifier but received ${a}`);const n=a;for(e.push(n);;){const t=this.buffer.read();if(this.buffer.next(),"group delimiter"===t.type&&")"===t.value)break;const r=t;if("separator"!==r.type)throw new Error(`expected comma but received ${r}`);const a=this.buffer.read();if(this.buffer.next(),"identifier"!==a.type)throw new Error(`expected identifier but received ${a}`);const n=a;e.push(n)}return e}parseInfixExpression(e){const t=this.buffer.read();if("group delimiter"===t.type&&"("===t.value)return"function expression"!==e.type&&"identifier"!==e.type?null:(this.buffer.next(),this.parseCall(e));if("operator"!==t.type)return null;const r=t.value;return"="===r&&"identifier"===e.type?(this.buffer.next(),this.parseAssignment(e)):"+"===r||"-"===r||"*"===r||"/"===r||"!="===r||"=="===r||">"===r||"<"===r||">="===r||"<="===r?(this.buffer.next(),this.parseArithmeticInfixExpression(e,r)):null}parseCall(e){const t=this.parseCallArguments();return(0,n.makeCall)(e,t)}parseCallArguments(){const e=this.buffer.read();if("group delimiter"===e.type&&")"===e.value)return this.buffer.next(),[];const t=[this.parseExpression(0)];for(;"separator"===this.buffer.read().type;){this.buffer.next();const e=this.parseExpression(0);t.push(e)}const r=this.buffer.read();if(this.buffer.next(),"group delimiter"!==r.type||")"!==r.value)throw new Error(`expect ) but received ${r.type}`);return t}parseAssignment(e){const t=s("="),r=this.parseExpression(t);return(0,n.makeAssignment)(e,r)}parseArithmeticInfixExpression(e,t){const r=s(t),a=this.parseExpression(r);return(0,n.makeInfixExpression)(t,e,a)}parseNumberLiteral(e){const t=Number(e);if(Number.isNaN(t))throw new Error(`expected non-NaN number, but received '${e}'`);return(0,n.makeNumberNode)(t)}parseBooleanLiteral(e){const t="참"===e;return(0,n.makeBooleanNode)(t)}parseStringLiteral(e){return(0,n.makeStringNode)(e)}}},813:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.makeAssignment=t.makeCall=t.makeFunctionExpression=t.makeInfixExpression=t.makePrefixExpression=t.makeStringNode=t.makeBooleanNode=t.makeNumberNode=t.makeIdentifier=void 0,t.makeIdentifier=e=>({type:"identifier",value:e}),t.makeNumberNode=e=>({type:"number node",value:e}),t.makeBooleanNode=e=>({type:"boolean node",value:e}),t.makeStringNode=e=>({type:"string node",value:e}),t.makePrefixExpression=(e,t)=>({type:"prefix expression",prefix:e,expression:t}),t.makeInfixExpression=(e,t,r)=>({type:"infix expression",infix:e,left:t,right:r}),t.makeFunctionExpression=(e,t=[])=>({type:"function expression",parameter:t,body:e}),t.makeCall=(e,t)=>({type:"call",functionToCall:e,callArguments:t}),t.makeAssignment=(e,t)=>({type:"assignment",left:e,right:t})},54:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.makeBlock=t.makeProgram=void 0,t.makeProgram=(e=[])=>({type:"program",statements:e}),t.makeBlock=(e=[])=>({type:"block",statements:e})},763:function(e,t,r){var a=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,n)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),n=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||a(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),n(r(813),t),n(r(602),t),n(r(54),t)},602:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.makeExpressionStatement=t.makeReturnStatement=t.makeBranchStatement=void 0,t.makeBranchStatement=(e,t,r)=>({type:"branch statement",predicate:e,consequence:t,alternative:r}),t.makeReturnStatement=e=>({type:"return statement",expression:e}),t.makeExpressionStatement=e=>({type:"expression statement",expression:e})},461:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{constructor(e){this.lexer=e,this.token=e.getToken()}isEnd(){return"end"===this.token.type}read(){return this.token}next(){this.token=this.lexer.getToken()}}}},t={},r=function r(a){var n=t[a];if(void 0!==n)return n.exports;var i=t[a]={exports:{}};return e[a].call(i.exports,i,i.exports,r),i.exports}(436);window.kal=r})(); \ No newline at end of file diff --git a/package.json b/package.json index 23a3c2b..0372888 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,13 @@ { "scripts": { - "build": "tsc", - "bundle": "webpack", + "build": "tsc && webpack && rm -rf tsc-out", "test": "jest", "test:watch": "jest --watchAll" }, + "browserslist": [ + "> 1%", + "not dead" + ], "devDependencies": { "@types/jest": "^29.5.10", "jest": "^29.7.0", diff --git a/tsconfig.json b/tsconfig.json index c18d48c..932814b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,12 @@ { "compilerOptions": { - "target": "es2016", + "target": "es2015", "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, - "outDir": "dist" + "outDir": "tsc-out" }, "include": [ "src/**/*" diff --git a/webpack.config.js b/webpack.config.js index c4ac1c9..18619c2 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,14 +1,15 @@ const { resolve } = require("path"); module.exports = { - entry: "./dist/index.js", + entry: "./tsc-out/index.js", output: { - filename: "index.js", - path: resolve("./bundle"), + filename: "index.min.js", + path: resolve("./dist"), library: { name: "kal", type: "window", }, }, + target: ["web", "es2015", "browserslist"], mode: "production", };