Beta reduction of S combinator in pure lambda calculus
Parentheses matter. Honestly, if you are unclear about how $\lambda$-calculus works, it is going to be rather hard to implement it.
Here is an explicit counter-example:
$$(K S)(K S) = S$$
but
$$K S K S = ((K S) K) S = S S \neq S.$$
I would recommend learning about parsers, abstract syntax trees, and other bits and pieces that make up an implementation of a programming language.
An example of the implementation of the $\lambda$-calculus is available as lambda at PL Zoo. See repl-in-browser for an implementation that runs in the browser (an instance of which is available here).