From ba559b3fa3fa49b159a3877868145630883a6c36 Mon Sep 17 00:00:00 2001
From: arookas <arookas@outlook.com>
Date: Mon, 1 Feb 2016 15:53:14 -0500
Subject: [PATCH] Compile only used functions/builtins

Uses iteration as to properly detect functions called only from within
other functions (which appear unused from the global scope).
---
 ssc/compiler.cs | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/ssc/compiler.cs b/ssc/compiler.cs
index de094ee..3712623 100644
--- a/ssc/compiler.cs
+++ b/ssc/compiler.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Diagnostics;
 using System.IO;
+using System.Linq;
 
 namespace arookas {
 	public class sunCompiler {
@@ -71,9 +72,15 @@ namespace arookas {
 			mBinary.WriteEND();
 		}
 		void CompileFunctions() {
-			foreach (var callable in mContext.SymbolTable.Callables) {
+			while (DoCompileFunctions() > 0) ;
+		}
+		int DoCompileFunctions() {
+			var count = 0;
+			foreach (var callable in mContext.SymbolTable.Callables.Where(i => i.HasRelocations && i.CompileCount == 0)) {
 				callable.Compile(this);
+				++count;
 			}
+			return count;
 		}
 		void CompileRelocations() {
 			foreach (var symbol in mContext.SymbolTable) {