@@ -3,6 +3,7 @@ import fs from 'node:fs';
33import { checker } from './utils.js' ;
44import { ERROR , SUCCESS } from './dictionary.js' ;
55import { ANAL_CHARACTERS } from './constants.js' ;
6+ import { matcher , sequence } from './regexp.js' ;
67
78export function anallify ( string ) {
89 if ( ! checker ( string ) ) {
@@ -34,38 +35,49 @@ export function stringify(anal) {
3435 throw new Error ( ERROR . missingArgument ) ;
3536 }
3637
38+ if ( ! matcher . test ( anal ) ) {
39+ throw new Error ( ERROR . notAcceptedByGrammar ) ;
40+ }
41+
3742 let string = '' ;
3843 const fragments = anal . split ( ' ' ) ;
39- const rule = new RegExp ( `${ ANAL_CHARACTERS } ` , 'g' ) ;
4044
4145 for ( let index = 0 ; index < fragments . length ; index += 1 ) {
4246 const fragment = fragments [ index ] ;
43- const character = String . fromCharCode ( ( ( fragment . match ( rule ) || [ ] ) . length ) ) ;
47+ const character = String . fromCharCode ( ( ( fragment . match ( sequence ) ) . length ) ) ;
4448 string = string . concat ( character ) ;
4549 }
4650
4751 return string ;
4852}
4953
5054export function run ( file ) {
55+ let contents = null ;
56+
5157 try {
52- const contents = fs . readFileSync ( file , { encoding : 'utf-8' } ) ;
53- return stringify ( contents ) ;
58+ contents = fs . readFileSync ( file , { encoding : 'utf-8' } ) ;
5459 } catch ( error ) {
5560 throw new Error ( ERROR . fileNotFound ) ;
5661 }
62+
63+ return stringify ( contents ) ;
5764}
5865
5966export function compile ( file ) {
67+ let contents = null ;
68+ const fileOptions = { encoding : 'utf-8' } ;
69+
6070 try {
61- const contents = fs . readFileSync ( file , { encoding : 'utf-8' } ) ;
62- let filename = file . split ( '.' ) ;
63- filename = filename . filter ( ( element , index ) => index < filename . length - 1 ) ;
64- filename = filename . join ( '.' ) ;
65- fs . writeFileSync ( `${ filename } .anal` , anallify ( contents ) , { encoding : 'utf-8' } ) ;
66- process . stdout . write ( `${ SUCCESS . compileSuccess } ` ) ;
67- return true ;
71+ contents = fs . readFileSync ( file , fileOptions ) ;
6872 } catch ( error ) {
6973 throw new Error ( ERROR . fileNotFound ) ;
7074 }
75+
76+ let filename = file . split ( '.' ) ;
77+ filename = filename . filter ( ( element , index ) => index < filename . length - 1 ) ;
78+ filename = filename . join ( '.' ) ;
79+
80+ fs . writeFileSync ( `${ filename } .anal` , anallify ( contents ) , fileOptions ) ;
81+ process . stdout . write ( `${ SUCCESS . compileSuccess } ` ) ;
82+ return true ;
7183}
0 commit comments