Skip to content

Instantly share code, notes, and snippets.

@eugenioenko
Created March 11, 2019 21:56
Show Gist options
  • Save eugenioenko/e1cfed27acf1ef4868adc0d4f7d3347f to your computer and use it in GitHub Desktop.
Save eugenioenko/e1cfed27acf1ef4868adc0d4f7d3347f to your computer and use it in GitHub Desktop.
Scaneando formula estequiometrica usando at-script
@isUpperCaseChar(char) => (char.match(#[A-Z]#g#)).size() !== 0;
@isLowerCaseChar(char) => (char.match(#[a-z]#g#)).size() !== 0;
@isNumberChar(char) => (char.match(#[0-9]#g#)).size() !== 0;
@scan(input) {
let i = 0;
let multiplier = 1;
let tokens = [];
while (i < input.size()) {
let char = input[i++];
if (isUpperCaseChar(char)) {
let name = char;
if (isLowerCaseChar(input[i])) {
name += input[i++];
}
tokens.push({
value: name,
count: 1 * multiplier,
type: 'atom'
});
} else if (isNumberChar(char)) {
let num = char;
while(i < input.size() && isNumberChar(input[i])) {
num += input[i++];
}
if (tokens[tokens.size() - 1] &&
tokens[tokens.size() - 1].type === 'atom'
) {
tokens[tokens.size() - 1].count *= num;
} else if (input[i] === '(' || isUpperCaseChar(input[i])) {
multiplier = num;
}
} else if (char === ')') {
multiplier = 1;
} else if (char === '+') {
multiplier = 1;
tokens.push({
value: '+',
type: 'addition'
});
} else if (char === '≅') {
tokens.push({
value: '≅',
type: 'equality'
});
}
}
tokens.push({
value: null,
type: 'eof'
});
return tokens;
}
print scan('NaOH + ClH ≅ H2O + NaCl');
echo(scan('NaOH + ClH ≅ H2O + NaCl'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment